home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / oxcc1434.zip / SRC / OXCCAI.C < prev    next >
C/C++ Source or Header  |  1996-09-03  |  123KB  |  5,780 lines

  1. /*
  2.     oxccai.c -- v1.430 ANF to intel asmcode generator
  3.  
  4.     Copyright (c) 1995
  5.     Norman D. Culver dba
  6.     Oxbow Software
  7.     1323 S.E. 17th Street #662
  8.     Ft. Lauderdale, FL 33316
  9.     (954) 463-4754
  10.     ndc@icanect.net
  11.     All rights reserved.
  12.  
  13.  * Redistribution and use in source and binary forms are permitted
  14.  * provided that: (1) source distributions retain this entire copyright
  15.  * notice and comment, and (2) distributions including binaries display
  16.  * the following acknowledgement:  ``This product includes software
  17.  * developed by Norman D. Culver dba Oxbow Software''
  18.  * in the documentation or other materials provided with the distribution
  19.  * and in all advertising materials mentioning features or use of this
  20.  * software.
  21.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  22.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  23.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  24.  
  25. */
  26. #define MAJOR_VERSION 1
  27. #define MINOR_VERSION 433
  28.  
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. #include <setjmp.h>
  33. #include <time.h>
  34.  
  35. #define SUPPORT_LONG_DOUBLE 0
  36. #define SUPPORT_LONG_LONG 1
  37.  
  38. #define NEED_FUNCTHUNK 1
  39. #define NEED_ANFDEFS 1
  40. #include "oxanf.h"
  41.     
  42. #define PROG oxccai
  43. #define USING_FRAMEWORK 1
  44. #define HOST_IS_LITTLE_ENDIAN 1
  45. #define REALLY_NEED_OFFSETS 0
  46. #define FUNCDATA (iv->category+1)
  47.  
  48. #define VFPRINTF(a,b) vfprintf(stderr,a,b)
  49. #define PERROR prerror
  50. #define PWARN prwarn
  51. #define PRINTF info
  52. static void prerror(const char *, ...);
  53. static void prwarn(const char *, ...);
  54. static void info(const char *, ...);
  55. int cfeprintf(const char *, ...);
  56.  
  57. #define FILEWRITE(buf, cnt)\
  58. {if(!iv->errors){if(fwrite(buf, 1, cnt, iv->outfile) != cnt)iv->errors = 12;}}
  59.  
  60. #define ROUNDING(a,b) ((a+(b-1))&~(b-1))
  61. #define ROUNDUP(a,b) a += ROUNDING(a,b)
  62.  
  63. #define KEYEQ(a,b) ((a)[0] == (b)[0] && (a)[1] == (b)[1])
  64. #define KEYLT(a,b) (((a)[1] < (b)[1]) || ((a)[1] == (b)[1] && (a)[0] < (b)[0]))
  65. #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  66.  
  67. /* ======================== CONCATENIZATION MACROS ==================== */
  68.  
  69. #define    _cat2_(a, b)    a##b
  70. #define _cat_(a, b)    _cat2_(a, b)
  71. #define Global(a) _cat_(PROG, a)
  72.  
  73. #define _pname2_(x)    #x
  74. #define _pname1_(x)    _pname2_(x)
  75. #define pName        _pname1_(PROG)
  76.  
  77.  
  78. /* ============== ENDIAN MACROS (input format is litle endian) ==== */
  79.  
  80. #if HOST_IS_LITTLE_ENDIAN
  81. #define GL(a) a
  82. #define GS(a) a
  83. #define PL(a) a
  84. #define PS(a) a
  85. #else
  86. #endif
  87.  
  88. /* =================== INPUT DATA FORMATS ========================== */
  89.  
  90. #define INFILE_SYMNUM 1
  91. #define OUTFILE_SYMNUM 2
  92.  
  93. /* ====================== STRUCTURES AND TYPEDEFS ======================== */
  94. typedef struct _jl {
  95.     struct _jl *next;
  96.     void *p;
  97.     char *q;
  98.     long *plabelval;
  99.     long offset;
  100. } *PJL;
  101.  
  102. typedef struct _el {
  103.     struct _el *next;
  104.     long spot;
  105.     short symnum;
  106. } *PEL;
  107.  
  108. typedef struct _afile {
  109.     unsigned char *file_p;
  110.     PopI header_p;
  111.     PopI size_p;
  112.     unsigned char *symtext_p;
  113.     unsigned char *prog_p;
  114.     unsigned char *data_p;
  115.     unsigned char *switch_p;
  116.     unsigned char *decl_p;
  117.     unsigned char *maxtemp_p;
  118.     unsigned char *seg_p;
  119.     unsigned char **symaddr;
  120.     unsigned char **decladdr;
  121.     unsigned long thunk_offset;
  122.     unsigned long bss_offset;
  123.     int maxtemp;
  124.     int maxtempclass;
  125.     void *datatbl;
  126.     short *symtran;
  127.     unsigned short *decltran;
  128.     int filenum;
  129.     int numsyms;
  130.     int numdecls;
  131.     int numrelocs;
  132.     int numsegs;
  133. } *Pafile;
  134.  
  135. typedef struct _iv {
  136.     int category;
  137.     FILE *outfile;
  138.     char *outname;
  139.     char *infile_name;
  140.     int remove_infile;
  141.     int argc;
  142.     char **argv;
  143.  
  144.     unsigned char **symaddr;
  145.     unsigned char **decladdr;
  146.  
  147.     struct _nodeO *ob_usedhead;
  148.     struct _nodeO *ob_usedtail;
  149.     struct _nodeO *ob;
  150.     unsigned char *ob_buf;
  151.     int ob_bufcnt;
  152.     int ob_nodecnt;
  153.     struct _nodeO *first_ob;
  154.  
  155.     struct _nodeC *cod_usedhead;
  156.     struct _nodeC *cod_usedtail;
  157.     struct _nodeC *cod;
  158.     unsigned char *cod_buf;
  159.     int cod_nodecnt;
  160.     int cod_bufcnt;
  161.     struct _nodeC *first_cod;
  162.     struct _nodeC *regcode;
  163.     long regsave;
  164.     long stksiz;
  165.  
  166.     int numfiles;
  167.     int lastlabel;
  168.     int labelnum;
  169.     int botlabel;
  170.     int errors;
  171.     int numsyms;
  172.     int numdecls;
  173.     int numsegs;
  174.     int maxtemp;
  175.     int maxtempclass;
  176.     unsigned long total_size;
  177.     unsigned long thunk_offset;
  178.     unsigned long bss_offset;
  179.     long first_temp;
  180.     long killop;
  181.     long numnested;
  182.     long lastline;
  183.     void *reloctbl;
  184.     void *newreloctbl;
  185.     void *extrntbl;
  186.     void *gbltbl;
  187.     void *symtbl;
  188.     void *labeltbl;
  189.     void *newlabeltbl;
  190.     void *tmptbl;
  191.     void *segtbl;
  192.     void *functbl;
  193.     void *finalsymtbl;
  194.     void *finalstringpack;
  195.     long finalpacksize;
  196.     void *datatbl;
  197.     void *builtintbl;
  198.     int in_builtin;
  199.     int has_structret;
  200.     int temps_written;
  201.  
  202.     PJL jbuf;
  203.     void *jbufstart;
  204.     int jmpcnt;
  205.     int jbufcnt;
  206.     long out_offset;
  207.     long func_offset;
  208.     int extmark;
  209.     short markedsym[10];
  210.     char *markedbuf[10];
  211.     int filenum;
  212.     Pafile files[1024];
  213.     char debug;
  214.     char only_debug;
  215.     char strip;
  216.     char listing_wanted;
  217.     char target_assembler;
  218.     char target_hardware;
  219.     char target_debugger;
  220.     char target_os;
  221.     char memory_model;
  222.     char obj_format;
  223. } *Piv;
  224.  
  225. struct _gloval {
  226.     char *symname;
  227.     int symnum;
  228.     unsigned char *p;
  229.     Pafile pf;
  230. };
  231. struct _dkey {
  232.     unsigned long offset;
  233.     long pad;
  234. };
  235. struct _dval {
  236. unsigned long size;
  237. unsigned char *p;
  238. unsigned char *prevp;
  239. long locid;
  240. short fileno;
  241. };
  242. struct _rkey {/* key area of reloctbl node */
  243.     unsigned long spot;
  244.     long fileno;
  245. };
  246. struct _rval {/* value area of reloctbl node */
  247.     unsigned char *p;
  248.     unsigned long base;
  249.     long offset;
  250.     short rsym;
  251.     unsigned char opcode;
  252.     char rsize;
  253.     short fileno;
  254. };
  255.  
  256.  
  257. /* Internal User API */
  258. static void *Cmalloc(int category, unsigned amount);
  259. static void *Ccalloc(int category, unsigned nelems, unsigned elemsize);
  260. static void *Crealloc(int category, void* buf, unsigned newsize);
  261. static void Cfree(int category, void* buf);
  262. static void Cfreecat(int category);
  263. static int Cmemrange(int category, unsigned* minp, unsigned* maxp);
  264. static int Cusedrange(int category, unsigned* minp, unsigned* maxp);
  265. static void Ctotrange(unsigned* minp,unsigned* maxp);
  266. static int Cnewcat(void);
  267. static void Cguard(int category);
  268. static void* NewSymTable(int category, int nbins);
  269. static int SymFind(void *tbl, void *key, void *result);
  270. static int SymFindRange(void *tbl, void *key, void *result);
  271. static void *SymInsert(void *tbl, void *key, void *value, int datsiz);
  272. static int StringInsert(void *tbl, char *string, void *result);
  273. static int StringFind(void *tbl, char *string, void *result);
  274. static void SymDelete(void *tbl, void *key);
  275. static int SymHead(void *tbl);
  276. static int SymNext(void *tbl);
  277. static void SymGetMark(void *tbl, void *markptr);
  278. static int SymMarkNext(void *tbl, void *mark);
  279. static void SymSetMark(void *tbl, void *markptr);
  280. static void SymKey(void *tbl, void *keyptr);
  281. static void SymValue(void *tbl, void *datptr);
  282. static void *seg_find(Piv iv, int id);
  283. static char *filenameof(char *path);
  284. static char *propernameof(Piv iv, char *name);
  285.  
  286. /* END: User API */
  287.  
  288. /* ====================== PUT UNIQUE CODE HERE ========================= */
  289. static void *do_stmt(Piv iv, unsigned char *p);
  290. static void *do_expr(Piv iv, unsigned char *p);
  291. static void do_bracket(Piv iv, unsigned char *p, unsigned char *q);
  292. static void *do_something(Piv iv, unsigned char *p);
  293. extern char *ctime();
  294.  
  295. /* ===================== ASMCODE OUTPUT GENERATOR ======================= */
  296. #define DUMP(args...) fprintf(((Piv)iv)->outfile, ## args)
  297. #define QDUMP(arg) fputs(arg, ((Piv)iv)->outfile)
  298. #define QDUMPC(arg) fputc(arg, ((Piv)iv)->outfile)
  299.  
  300. #define SOURCE 0
  301. #define DESTINATION 1
  302. #define USE_ADDR (0x0001)
  303. #define NEEDS_CONVERSION (0x0002)
  304.  
  305. #define BWORD (1)
  306. #define SWORD (2)
  307. #define LWORD (3)
  308.  
  309. #define INDIRECT (0x01)
  310. #define INDEXED (0x02)
  311.  
  312. /* WARNING ONLY GCC WILL COMPILE THIS */
  313. #define ENCODE(args...) \
  314. {iv->cod->ee = (struct _nodeE){## args}; link_cod(iv);}
  315.  
  316. typedef struct _nodeE
  317. {
  318.     unsigned short inst;
  319.     unsigned char size;
  320.     unsigned char s1;
  321.     unsigned char s1m;
  322.     unsigned char s2;
  323.     unsigned char s2m;
  324.     unsigned char d1;
  325.     unsigned char d1m;
  326.     unsigned char d2;
  327.     unsigned char d2m;
  328.     unsigned char cdat;
  329.     long dat;
  330.     void *ptr;
  331. } NODEE, *PNODEE;
  332.  
  333. typedef struct _nodeC
  334. {
  335.     struct _nodeC *next;
  336.     struct _nodeE ee;
  337. } NODEC, *PNODEC;
  338.  
  339. typedef struct _nodeO 
  340. {
  341.     struct _nodeO *next;
  342.     unsigned char *p;
  343.     ND d;
  344.     ND l;
  345.     ND r;
  346.     PNODEC startinst;
  347.     PNODEC endinst;
  348. } NODEO, *PNODEO;
  349.  
  350. static char *notice = 
  351.     "  Generated by Oxbow Software Asmcode Backend `oxccai' version %d.%d\n\n";
  352.  
  353. static char *opnames[] = {"",
  354.   "aaa","aad","aam","aas","adc","add","and","arpl",
  355.   "bound","bsf","bsr","bswap","bt","btc","btr","bts",
  356.   "call","cbw","cdq","clc","cld","cli","clts",
  357.   "cmc","cmp","cmps","cmpxchg","cwd","cwde",
  358.   "daa","das","dec","div",
  359.   "enter","esc",
  360.   "hlt",
  361.   "idiv","imul","in","inc","ins",
  362.   "int","into","invd","invlpg","iret","iretd","iretdf","iretf",
  363.   "ja","jae","jb","jbe","jc","jcxz","je","jecxz","jg","jge","jl","jle","jmp",
  364.   "jna","jnae","jnb","jnbe","jnc","jne","jng","jnge","jnl","jnle","jno","jnp",
  365.   "jns","jnz","jo","jp","jpe","jpo","js","jz",
  366.   "lar","lds","lea","leave","les","lfs",
  367.   "lgdt","lgs","lidt","lldt","lmsw","lock",
  368.   "lods","loop","loope","loopne","loopnz","loopz",
  369.   "lsl","lss","ltr",
  370.   "mov","movs","movsx","movzx","mul",
  371.   "neg","nop","not",
  372.   "or","out","outs",
  373.   "pop","popa","popf","push","pusha","pushf",
  374.   "rcl","rcr","ret","retf","retn","rol","ror",
  375.   "rep","repe","repne","repnz","repz",
  376.   "sahf","sal","sar","sbb","scas","shl","shr",
  377.   "seta","setae","setb","setbe","setc","sete","setg","setge","setl","setle",
  378.   "setna","setnae","setnb","setnbe","setnc","setne","setng","setnge","setnl",
  379.   "setnle","setno","setnp","setns","setnz","seto","setp","setpe","setpo",
  380.   "sets","setz","sgdt","sidt","sldt","smsw",
  381.   "stc","std","sti","stos","str","sub",
  382.   "test",
  383.   "verr","verw",
  384.   "wait","wbinvd",
  385.   "xadd","xchg","xlat","xlatb","xor",
  386.  
  387.   "fabs","fadd","faddp","fbld","fbstp","fchs","fclex",
  388.   "fcom","fcomp","fcompp","fcos",
  389.   "fdecstp","fdisi",
  390.   "fdiv","fdivp","fdivr","fdivrp","feni","ffree","fiadd",
  391.   "ficom","ficomp","fidiv","fidivr","fild","fimul","fincstp","finit",
  392.   "fist","fistp","fisub","fisubr",
  393.   "fld","fld1","fldcw","fldenv","fldlg2","fldln2","fldl2e","fldl2t","fldpi","fldz",
  394.   "fmul","fmulp",
  395.   "fnclex","fndisi","fneni","fninit","fnop","fnsave","fnstcw","fnstenv","fnsts",
  396.   "fpatan","fprem","fprem1","fptan","frndint","frstor","fsave",
  397.   "fscale","fsin","fsincos","fsqrt","fst","fstcw","fstenv",
  398.   "fstp",
  399.   "fstsw","fsub","fsubr",
  400.   "ftst","fucom","fucomp","fucompp","fwait","fxam","fxch","fxtract",
  401.   "fyl2x","fyl2xpi","f2xmi"
  402. };
  403. enum  {        
  404.   aaa=1,aad,aam,aas,adc,add,and,arpl,
  405.   bound,bsf,bsr,bswap,bt,btc,btr,bts,
  406.   call,cbw,cdq,clc,cld,cli,clts,
  407.   cmc,cmp,cmps,cmpxchg,cwd,cwde,
  408.   daa,das,dec,DIV,
  409.   enter,esc,
  410.   hlt,
  411.   idiv,imul,in,inc,ins,
  412.   INT,into,invd,invlpg,iret,iretd,iretdf,iretf,
  413.   ja,jae,jb,jbe,jc,jcxz,je,jecxz,jg,jge,jl,jle,jmp,
  414.   jna,jnae,jnb,jnbe,jnc,jne,jng,jnge,jnl,jnle,jno,jnp,
  415.   jns,jnz,jo,jp,jpe,jpo,js,jz,
  416.   lar,lds,lea,leave,les,lfs,
  417.   lgdt,lgs,lidt,lldt,lmsw,lock,
  418.   lods,loop,loope,loopne,loopnz,loopz,
  419.   lsl,lss,ltr,
  420.   mov,movs,movsx,movzx,mul,
  421.   neg,nop,not,
  422.   or,out,outs,
  423.   pop,popa,popf,push,pusha,pushf,
  424.   rcl,rcr,ret,retf,retn,rol,ror,
  425.   rep,repe,repne,repnz,repz,
  426.   sahf,sal,sar,sbb,scas,shl,shr,
  427.   seta,setae,setb,setbe,setc,sete,setg,setge,setl,setle,
  428.   setna,setnae,setnb,setnbe,setnc,setne,setng,setnge,setnl,
  429.   setnle,setno,setnp,setns,setnz,seto,setp,setpe,setpo,
  430.   sets,setz,sgdt,sidt,sldt,smsw,
  431.   stc,std,sti,stos,str,sub,
  432.   test,
  433.   verr,verw,
  434.   WAIT,wbinvd,
  435.   xadd,xchg,xlat,xlatb,xor,
  436.  
  437.   fabs,fadd,faddp,fbld,fbstp,fchs,fclex,
  438.   fcom,fcomp,fcompp,fcos,
  439.   fdecstp,fdisi,
  440.   fdiv,fdivp,fdivr,fdivrp,feni,ffree,fiadd,
  441.   ficom,ficomp,fidiv,fidivr,fild,fimul,fincstp,finit,
  442.   fist,fistp,fisub,fisubr,
  443.   fld,fld1,fldcw,fldenv,fldlg2,fldln2,fldl2e,fldl2t,fldpi,fldz,
  444.   fmul,fmulp,
  445.   fnclex,fndisi,fneni,fninit,fnop,fnsave,fnstcw,fnstenv,fnsts,
  446.   fpatan,fprem,fprem1,fptan,frndint,frstor,fsave,
  447.   fscale,fsin,fsincos,fsqrt,fst,fstcw,fstenv,
  448.   fstp,
  449.   fstsw,fsub,fsubr,
  450.   ftst,fucom,fucomp,fucompp,fwait,fxam,fxch,fxtract,
  451.   fyl2x,fyl2xpi,f2xmi,
  452.   ENDCODES,
  453.   ALIGN,PUSHREGS,POPREGS,LABEL,LINE,FUNCNAME,LINEFEED
  454. };
  455. static char *udatnames[] = {"",
  456. "%al","%ah","%ax","%eax","%bl","%bh","%bx","%ebx","%cl","%ch","%cx","%ecx",
  457. "%dl","%dh","%dx","%edx","%sp","%esp","%bp","%ebp",
  458. "%si","%esi","%di","%edi","%ss","%ds","%es","%fs","%gs","","",""
  459. };
  460. static char *datnames[] = {"",
  461. "al","ah","ax","eax","bl","bh","bx","ebx","cl","ch","cx","ecx",
  462. "dl","dh","dx","edx","sp","esp","bp","ebp",
  463. "si","esi","di","edi","ss","ds","es","fs","gs","","",""
  464. };
  465. enum {
  466. AL=1,AH,AX,EAX,BL,BH,BX,EBX,CL,CH,CX,ECX,DL,DH,DX,EDX,SP,ESP,BP,EBP,
  467. SI,ESI,DI,EDI,SS,DS,ES,FS,GS,CONST,VARNAME,LOCATION
  468. };
  469.  
  470. static char uwordsize[] = {' ','b','w','l'};
  471. static char dwordsize[] = {' ','b','w','d'};
  472.  
  473. static long
  474. symnumof(Piv iv, char *symb)
  475. {
  476. struct _gloval *valp;
  477.  
  478.     if(StringFind(iv->gbltbl, symb, &valp))
  479.         return (long)valp->pf->symtran[valp->symnum];
  480.     return 0;
  481.  
  482. }
  483. static void
  484. buildin(Piv iv, char *symb, unsigned char code)
  485. {
  486. long key[2];
  487.  
  488.     if((key[0] = symnumof(iv, symb)))
  489.     {
  490.         key[1] = 0;
  491.         SymInsert(iv->builtintbl, key, &code, 1);
  492.     }
  493. }
  494.  
  495.  
  496. static void
  497. install_builtins(Piv iv)
  498. {/* USE THIS TO INSTALL WHATEVER BUILTINS ARE IN THE TARGET INTERPRETER */
  499. #define BUILDIN(a,b) buildin(iv,#a,b)
  500.  
  501.     iv->builtintbl = NewSymTable(iv->category, 191);
  502.  
  503.     BUILDIN(alloca,0);
  504.  
  505. #undef BUILDIN
  506. }
  507. static long
  508. final_strofs(Piv iv, char *string)
  509. {
  510. long *result;
  511.  
  512.     if(StringFind(iv->finalsymtbl, string, &result))
  513.         return result[2];    
  514.     return 0;
  515. }
  516. static short
  517. final_symnum(Piv iv, short symnum)
  518. {
  519. long *result;
  520.  
  521.     if(StringFind(iv->finalsymtbl, iv->symaddr[symnum], &result))
  522.         return result[1]-1;
  523.     return 0;
  524. }
  525. static void
  526. make_final_symtab(Piv iv)
  527. {
  528. int i;
  529.     iv->finalsymtbl = NewSymTable(iv->category, 0);
  530.     if(SymHead(iv->gbltbl))
  531.     {
  532.         i = 0;
  533.         while(SymNext(iv->gbltbl))
  534.         {
  535.         long *result;
  536.         struct _gloval *valp;
  537.  
  538.             SymValue(iv->gbltbl, &valp);
  539.             if(*(valp->p))
  540.             {
  541.                 if(!StringInsert(iv->finalsymtbl, valp->symname, &result))
  542.                 {/* New Entry */
  543.                     result[1] = ++i;
  544.                 }
  545.             }
  546.         }
  547.     }
  548. }
  549. static void *
  550. new_nodeO(Piv iv)
  551. {
  552. PNODEO p;
  553.  
  554.     if(iv->ob_bufcnt < sizeof(NODEO))
  555.     {/* Allocate a new chunk of linked list space */
  556.       iv->ob_bufcnt = 4080;
  557.       iv->ob_buf = Ccalloc(FUNCDATA, 1, iv->ob_bufcnt);
  558.     }
  559.     p = (PNODEO)iv->ob_buf;
  560.     iv->ob_buf += sizeof(NODEO);
  561.     iv->ob_bufcnt -= sizeof(NODEO);
  562.     ++iv->ob_nodecnt;
  563.     return p;    
  564. }
  565. static void
  566. link_ob(Piv iv)
  567. {/* Attach to the used list */
  568.  
  569.     if(!iv->ob_usedhead)
  570.     {
  571.         iv->ob_usedhead = iv->ob;
  572.         iv->ob_usedtail = iv->ob;
  573.     }
  574.     else
  575.     {
  576.         iv->ob_usedtail->next = iv->ob;
  577.         iv->ob_usedtail = iv->ob;
  578.     }
  579.     iv->ob = new_nodeO(iv);
  580. }
  581. static void *
  582. new_nodeC(Piv iv)
  583. {
  584. PNODEC p;
  585.  
  586.     if(iv->cod_bufcnt < sizeof(NODEC))
  587.     {/* Allocate a new chunk of linked list space */
  588.       iv->cod_bufcnt = 4080;
  589.       iv->cod_buf = Ccalloc(FUNCDATA, 1, iv->cod_bufcnt);
  590.     }
  591.     p = (PNODEC)iv->cod_buf;
  592.     iv->cod_buf += sizeof(NODEC);
  593.     iv->cod_bufcnt -= sizeof(NODEC);
  594.     ++iv->cod_nodecnt;
  595.     return p;    
  596. }
  597. static void
  598. link_cod(Piv iv)
  599. {/* Attach to the used list */
  600.  
  601.     if(!iv->cod_usedhead)
  602.     {
  603.         iv->cod_usedhead = iv->cod;
  604.         iv->cod_usedtail = iv->cod;
  605.     }
  606.     else
  607.     {
  608.         iv->cod_usedtail->next = iv->cod;
  609.         iv->cod_usedtail = iv->cod;
  610.     }
  611.     iv->cod = new_nodeC(iv);
  612. }
  613. static PNODEC
  614. gen_inst(Piv iv, PNODEO pnode)
  615. {
  616. unsigned char opcode;
  617. unsigned char *p;
  618. PND d,l,r;
  619.  
  620.         d = &pnode->d;
  621.         l = &pnode->l;
  622.         r = &pnode->r;
  623.         p = pnode->p;
  624.         opcode = *p;
  625.  
  626.         switch(opcode)
  627.         {
  628.             /* 0 ADDRESS MODE */
  629.             case regainop:
  630.             case grabop:
  631.                 break;
  632.             case retvoidop:
  633.                 ENCODE(jmp, LWORD, s1: LOCATION, dat: iv->botlabel);
  634.                 break;
  635.  
  636.             /* 1 ADDRESS MODE */
  637.             case jmploopop:
  638.             case jmpcontinueop:
  639.             case jmpbreakop:
  640.             case jmpgotoop:
  641.                 ENCODE(jmp, LWORD, s1: LOCATION, dat: pnode->d.data->Uulong);
  642.                 break;
  643.             case ljmptrueop:
  644.             case ljmpfalseop:
  645.             case jmptrueop:
  646.             case jmpfalseop:
  647.                 break;
  648.             case retdataop:
  649.                 break;
  650.  
  651.             /* 2 ADDRESS MODE */
  652.             case getvalop:
  653.             case derefop:
  654.             case assignop:
  655.             case duptmpop:
  656.             case truthop:
  657.             case negop:
  658.             case complop:
  659.             case notop:
  660.             case copyop:
  661.             case castop:
  662.             case clrdatop:
  663.             case compsavop:
  664.             case totmpop:
  665.             case retstructop:
  666.             case switchop:
  667.             case argop:
  668.                 break;
  669.  
  670.             /* 3 ADDRESS MODE */
  671.             case plusop:
  672.             case minusop:
  673.             case mulop:
  674.             case divop:
  675.             case lshop:
  676.             case rshop:
  677.             case modop:
  678.             case orop:
  679.             case xorop:
  680.             case andop:
  681.                 break;
  682.             case eqop:
  683.             case neqop:
  684.             case ltop:
  685.             case gtop:
  686.             case leop:
  687.             case geop:
  688.                 break;
  689.             case getbitfieldop:
  690.             case putbitfieldop:
  691.                 break;
  692.             case callfuncop:
  693.                 break;
  694.  
  695.             default:
  696.                 break;
  697.         }
  698.         return iv->cod_usedtail;
  699. }
  700. static int
  701. get_size(int type, int size)
  702. {
  703.     switch(type)
  704.     {
  705.         case    D_ARRAY:
  706.         case    D_STRUCT:
  707.         case    D_FUNCTION:
  708.             return 4;
  709.  
  710.         default:
  711.             return size;
  712.     }
  713.     return 0;
  714. }
  715. static void
  716. set_op(PND pnd, PopA ptr, unsigned char otype)
  717. {
  718. unsigned char optype  = otype & 0xe0;
  719. unsigned char isize = otype & 0x1f;
  720.  
  721.     pnd->data = (DATUM *)ptr;
  722.     if(optype <= OPIMMED4)
  723.     {
  724.         pnd->dtype = optype >> 5;
  725.         pnd->quals = Q_CONST;
  726.         pnd->size = isize;
  727.         pnd->opsize = get_size(pnd->dtype, isize);
  728.         pnd->atype = A_IMMED;
  729. #if 0
  730.         switch(pnd->dtype)
  731.         {
  732.             case D_SIGNED:
  733.             {
  734.                 if(isize > 8)
  735.                 {
  736.                     memcpy(&pnd->data, ptr, isize);
  737.                 }
  738.                 else if(isize == 8)
  739.                 {
  740.                     pnd->data.Udouble = *((double*)ptr);
  741.                 }
  742.                 else
  743.                 {
  744.                 long l[2];
  745.                     l[0] = 0;
  746.                     l[1] = 0;
  747.                     if(isize == 4)
  748.                         l[0] = *((long*)ptr);
  749.                     else if(isize == 2)
  750.                         l[0] = *((short*)ptr);
  751.                     else if(isize == 1)
  752.                         l[0] = *((char*)ptr);
  753.                     if(l[0] < 0)
  754.                         l[1] = -1;
  755.                     pnd->data.Udouble = *((double*)l);
  756.                 }
  757.                 break;
  758.             }
  759.             case D_UNSIGNED:
  760.             case D_POINTER:
  761.             {
  762.                 if(isize > 8)
  763.                 {
  764.                     memcpy(&pnd->data, ptr, isize);
  765.                 }
  766.                 else if(isize == 8)
  767.                 {
  768.                     pnd->data.Udouble = *((double*)ptr);
  769.                 }
  770.                 else
  771.                 {
  772.                 unsigned long l[2];
  773.                     l[0] = 0;
  774.                     l[1] = 0;
  775.                     if(isize == 4)
  776.                         l[0] = *((unsigned long*)ptr);
  777.                     else if(isize == 2)
  778.                         l[0] = *((unsigned short*)ptr);
  779.                     else if(isize == 1)
  780.                         l[0] = *((unsigned char*)ptr);
  781.                     pnd->data.Udouble = *((double*)l);
  782.                 }
  783.                 break;
  784.             }
  785.             case D_FLOAT:
  786.             {
  787.                 if(isize == 4)
  788.                 {
  789.                     pnd->data.Udouble = *((float*)ptr);
  790.                 }
  791.                 else if(isize > 8)
  792.                 {
  793. #if SUPPORT_LONG_DOUBLE
  794.                     pnd->data.Ulongdouble = *((long double)ptr);
  795. #else
  796.                     pnd->data.Udouble = 0.0;
  797. #endif
  798.                 }
  799.                 else
  800.                 {
  801.                     pnd->data.Udouble = *((double*)ptr);
  802.                 }
  803.                 break;
  804.             }
  805.         }/* END: switch */
  806. #endif
  807.     } /* END: OPIMMED */
  808.     else
  809.     {
  810.     unsigned short type = GS(ptr->dtype);
  811.         pnd->dtype = type & 0xff;
  812.         pnd->quals = (type >> 8) & 0xff;
  813.         pnd->size = GL(ptr->dsize);
  814.         pnd->opsize = get_size(pnd->dtype, pnd->size);
  815.         pnd->atype = GS(ptr->atype);
  816.  
  817.         if(optype == OPAUTO)
  818.         {
  819.             if(GL(ptr->pofs) >= 0)
  820.             {
  821.                 pnd->atype &= ~A_AUTO;
  822.                 pnd->atype |= A_PARAM;
  823.             }
  824.         }
  825.     }
  826. }
  827. static void *
  828. decode_anf(Piv iv, unsigned char *p)
  829. {
  830. void *dptr;
  831. void *lptr;
  832. void *rptr;
  833.  
  834.     if(iv->debug >= '1')
  835.         cfeprintf("DECODE inst(%u) `%s'\n", *p, oxgenops[*p]);
  836.  
  837.     if(*p == 0)
  838.         return POP->next;
  839.  
  840.     iv->ob->p = p;
  841.     switch(*p)
  842.     {
  843.         case regainop:
  844.         case grabop:
  845.         case retvoidop:
  846.         {/* 0 address mode */
  847.             break;
  848.         }
  849.         case jmploopop:
  850.         case jmpcontinueop:
  851.         case jmpbreakop:
  852.         case jmpgotoop:
  853.         case ljmptrueop:
  854.         case jmptrueop:
  855.         case ljmpfalseop:
  856.         case jmpfalseop:
  857.         case funcstartop:
  858.         case funcstopop:
  859.         case retdataop:
  860.         {/* 1 address mode */
  861.             dptr = (p+8);
  862.             set_op(&iv->ob->d, dptr, p[1]);
  863.             break;
  864.         }
  865.         case getvalop:
  866.         case derefop:
  867.         case assignop:
  868.         case duptmpop:
  869.         case truthop:
  870.         case aliastmpop:
  871.         case negop:
  872.         case complop:
  873.         case notop:
  874.         case copyop:
  875.         case clrdatop:
  876.         case compsavop:
  877.         case totmpop:
  878.         case retstructop:
  879.         case switchop:
  880.         case argop:
  881.         case castop:
  882.         {/* 2 address mode */
  883.             dptr = (p+8);
  884.             lptr = (((char*)dptr) + (p[1] & 0x1f));
  885.             set_op(&iv->ob->d, dptr, p[1]);
  886.             set_op(&iv->ob->l, lptr, p[2]);
  887.             break;
  888.         }
  889.         case plusop:
  890.         case minusop:
  891.         case mulop:
  892.         case divop:
  893.         case modop:
  894.         case orop:
  895.         case xorop:
  896.         case andop:
  897.         case eqop:
  898.         case neqop:
  899.         case ltop:
  900.         case gtop:
  901.         case leop:
  902.         case geop:
  903.         case lshop:
  904.         case rshop:
  905.         case getbitfieldop:
  906.         case callfuncop:
  907.         case putbitfieldop:
  908.         {/* 3 address mode */
  909.             dptr = (p+8);
  910.             lptr = (((char*)dptr) + (p[1] & 0x1f));
  911.             rptr = (((char*)lptr) + (p[2] & 0x1f));
  912.             set_op(&iv->ob->d, dptr, p[1]);
  913.             set_op(&iv->ob->l, lptr, p[2]);
  914.             set_op(&iv->ob->r, rptr, p[3]);
  915.             break;
  916.         }
  917.         default:
  918.         {
  919.             return POP->next;
  920.         }
  921.     }/* END: switch(*p) */
  922.  
  923.     /* Save pointers to the machine instructions generated by this ANF code */
  924.     iv->ob->startinst = iv->cod;
  925.     iv->ob->endinst = gen_inst(iv, iv->ob);
  926.  
  927.     if(iv->cod != iv->ob->startinst)
  928.     {/* instructions were generated */
  929.         link_ob(iv);
  930.     }
  931.     return POP->next;
  932. }/* END: decode_anf() */
  933.  
  934. static void *
  935. skip_bracket(unsigned char *p)
  936. {
  937. long opcode = *p;
  938. int opcount = 0;
  939.  
  940.     for(;;)
  941.     {
  942.         if(*p == opcode)
  943.             ++opcount;
  944.         else if(*p == endop && GL(POP->data) == opcode)
  945.         {
  946.             if(--opcount == 0) {
  947.                 return p;
  948.             }
  949.         }
  950.         else if(*p == endfileop || *p == endallop)
  951.         {
  952.             PERROR(pName ":ERROR: Malformed input file3=%u=%p",*p, p);
  953.         }
  954.         p = POP->next;
  955.     }
  956.     return 0;
  957. }
  958. static void *
  959. do_arrayelem(Piv iv, unsigned char *p)
  960. {/* Arrange output for the stack machine */
  961. void *sp = p;
  962. unsigned char *q = skip_bracket(p);
  963. void *sadims[10];
  964. void *sadimsq[10];
  965. void *spdims[10];    
  966. void *spdimsq[10];
  967. int sacnt, spcnt, i;
  968.  
  969.     /* Scan over the bracket and pick up the dimension computations */
  970.     sacnt = spcnt = 0;
  971.     p = POP->next;
  972.     while(p < q)
  973.     {
  974.         if(*p == arraydimsop)
  975.         {
  976.         void *qq = skip_bracket(p);
  977.             sadims[sacnt] = p;
  978.             sadimsq[sacnt++] = qq;
  979.             p = qq;
  980.         }
  981.         else if(*p == ptrdimsop)
  982.         {
  983.         void *qq = skip_bracket(p);
  984.             spdims[spcnt] = p;
  985.             spdimsq[spcnt++] = qq;
  986.             p = qq;
  987.         }
  988.         p = POP->next;
  989.     }
  990.     /* Dump the dimension computations in stack order */
  991.     for(i = spcnt-1; i >= 0; --i)
  992.     {
  993.         do_bracket(iv, spdims[i], spdimsq[i]);
  994.     }
  995.     for(i = sacnt-1; i >= 0; --i)
  996.     {
  997.         do_bracket(iv, sadims[i], sadimsq[i]);
  998.     }
  999.  
  1000.     /* Dump the remainder of the array element bracket */
  1001.     p = sp;
  1002.     sacnt = spcnt = 0;
  1003.     p = POP->next;
  1004.      while(p < q)
  1005.     {
  1006.         if(*p == arraydimsop)
  1007.             p = ((Pop)sadimsq[sacnt++])->next;
  1008.         else if(*p == ptrdimsop)
  1009.             p = ((Pop)spdimsq[spcnt++])->next;
  1010.         else
  1011.             p = do_something(iv, p);
  1012.     }
  1013.     return ((Pop)q)->next;
  1014. }
  1015. static int
  1016. funcret_used(PopT op, unsigned char *p)
  1017. {
  1018. long tmpnum;
  1019.     if(        op->dtype == 0
  1020.         &&    op->dsize == 0)
  1021.     {
  1022.         return 1;                        /* void function */
  1023.     }
  1024.     tmpnum = op->tmpnum;
  1025.  
  1026.     while(*p != funcexitop)
  1027.     {
  1028.         if(*p && *p <= (unsigned char)100)
  1029.         {
  1030.         unsigned char *qp = p+8;
  1031.             if(*p == callfuncop)
  1032.             {/* function using same temp */
  1033.                 if(GL(((PopT)qp)->tmpnum) == tmpnum)
  1034.                 {
  1035.                     return 1;        /* not used earlier */
  1036.                 }
  1037.             }
  1038.             else
  1039.             {
  1040.                 if((p[1]&0xe0) == OPRET)
  1041.                 {
  1042.                      if(GL(((PopT)qp)->tmpnum) == tmpnum)
  1043.                     {
  1044.                         return 0;            /* ret used */
  1045.                     }
  1046.                 }                
  1047.                 qp += p[1]&0x1f;
  1048.                 if((p[2]&0xe0) == OPRET)
  1049.                 {
  1050.                     if(GL(((PopT)qp)->tmpnum) == tmpnum)
  1051.                     {
  1052.                         return 0;            /* ret used */
  1053.                     }
  1054.                 }
  1055.                 qp += p[2]&0x1f;
  1056.                 if((p[3]&0xe0) == OPRET)
  1057.                 {
  1058.                     if(GL(((PopT)qp)->tmpnum) == tmpnum)
  1059.                     {
  1060.                         return 0;            /* ret used */
  1061.                     }
  1062.                 }
  1063.             }
  1064.         }
  1065.         p = POP->next;
  1066.     }
  1067.     return 1;                    /* ret not used */
  1068. }
  1069. static unsigned char
  1070. check_for_builtin(Piv iv, unsigned char *p, int flag)
  1071. {
  1072. short symnum;
  1073. long key[2];
  1074. unsigned char *result;
  1075.  
  1076.     if(flag == 0)
  1077.     {
  1078.         symnum = GS(((PopA)(p+20))->symnum);
  1079.     }
  1080.     else if(flag == 1)
  1081.     {
  1082.         symnum = GS(((PopI)(p+16))->s.symnum);
  1083.     }
  1084.     else if(flag == 2)
  1085.     {
  1086.         symnum = GS(POPI->s.symnum);
  1087.     }
  1088.     else return 0;
  1089.  
  1090.     key[0] = symnum;
  1091.     key[1] = 0;
  1092.     if(SymFind(iv->builtintbl, key, &result) == 1)
  1093.         return *result;
  1094.     return 0;
  1095. }
  1096. static void *
  1097. do_funcall(Piv iv, unsigned char *p)
  1098. {
  1099. void *sp = p;
  1100. unsigned char *q = skip_bracket(p);
  1101. int argcnt, i, dump;
  1102. void *argloads[100];
  1103. void *argloadsq[100];
  1104. char *callop = 0;
  1105. unsigned char builtin = 0;
  1106.  
  1107.     /* Scan over the bracket and pick up argument loads */
  1108.     argcnt = 0;
  1109.     p = POP->next;
  1110.     while(p < q)
  1111.     {
  1112.         if(*p == getvalop && callop == 0)
  1113.         {
  1114.             builtin = check_for_builtin(iv, p, 0);
  1115.         }
  1116.         else if(*p == callfuncop)
  1117.             callop = p;
  1118.         else if(*p == argloadop)
  1119.         {
  1120.         void *qq = skip_bracket(p);
  1121.             argloads[argcnt] = p;
  1122.             argloadsq[argcnt++] = qq;
  1123.             p = qq;
  1124.         }
  1125.         p = POP->next;
  1126.     }
  1127.  
  1128.     /* Check out whether the function return is used */
  1129.     dump = funcret_used((PopT)(callop+8), q);
  1130.  
  1131.     /* Calling an interpreter builtin ?? */
  1132.     if(builtin)
  1133.     {
  1134.         iv->in_builtin = 1;
  1135.  
  1136.         /* Dump the argument loads in order */
  1137.         /* The ARG instruction will be suppressed because iv->in_builtin > 0 */
  1138.         for(i = 0; i < argcnt; ++i)
  1139.         {
  1140.             do_bracket(iv, argloads[i], argloadsq[i]);
  1141.         }
  1142.     }
  1143.     else
  1144.     {
  1145.         /* Generate the CALLSETUP instruction */
  1146.         p = sp;
  1147.         p = POP->next;
  1148.         while(p < q)
  1149.         {
  1150.             if(*p == argloadop)
  1151.                 break;
  1152.             else
  1153.                 p = do_something(iv, p);
  1154.         }            
  1155.  
  1156.         /* Dump the argument loads in order */
  1157.         for(i = 0; i < argcnt; ++i)
  1158.         {
  1159.             do_bracket(iv, argloads[i], argloadsq[i]);
  1160.         }
  1161.         /* Generate the CALL instruction */
  1162.     }
  1163.     return ((Pop)q)->next;
  1164. }
  1165. static void *
  1166. do_cond(Piv iv, unsigned char *p)
  1167. {
  1168. unsigned char *q = skip_bracket(p);
  1169.  
  1170.     p = POP->next;
  1171.     while(p < q)
  1172.     {
  1173.       p = do_something(iv, p);
  1174.     }
  1175.     return ((Pop)q)->next;
  1176. }
  1177. static void *
  1178. do_twopath(Piv iv, unsigned char *p)
  1179. {
  1180. unsigned char *q = skip_bracket(p);
  1181.  
  1182.     p = POP->next;
  1183.     while(p < q)
  1184.     {
  1185.       p = do_something(iv, p);
  1186.     }
  1187.     return ((Pop)q)->next;
  1188. }
  1189. static void *
  1190. do_logical(Piv iv, unsigned char *p)
  1191. {
  1192. unsigned char *q = skip_bracket(p);
  1193.  
  1194.     p = POP->next;
  1195.     while(p < q)
  1196.     {
  1197.       p = do_something(iv, p);
  1198.     }
  1199.     return ((Pop)q)->next;
  1200. }
  1201. static void *
  1202. do_binop(Piv iv, unsigned char *p)
  1203. {
  1204. unsigned char *q = skip_bracket(p);
  1205.  
  1206.     p = POP->next;
  1207.     while(p < q)
  1208.     {
  1209.       p = do_something(iv, p);
  1210.     }
  1211.     return ((Pop)q)->next;
  1212. }
  1213. static void *
  1214. do_strelem(Piv iv, unsigned char *p)
  1215. {
  1216. unsigned char *q = skip_bracket(p);
  1217.  
  1218.     p = POP->next;
  1219.     while(p < q)
  1220.     {
  1221.       p = do_something(iv, p);
  1222.     }
  1223.     return ((Pop)q)->next;
  1224. }
  1225. static void *
  1226. do_ptrelem(Piv iv, unsigned char *p)
  1227. {
  1228. unsigned char *q = skip_bracket(p);
  1229.  
  1230.     p = POP->next;
  1231.     while(p < q)
  1232.     {
  1233.       p = do_something(iv, p);
  1234.     }
  1235.     return ((Pop)q)->next;
  1236. }
  1237. static void *
  1238. do_argload(Piv iv, unsigned char *p)
  1239. {
  1240. unsigned char *q = skip_bracket(p);
  1241.  
  1242.     p = POP->next;
  1243.     while(p < q)
  1244.     {
  1245.       p = do_something(iv, p);
  1246.     }
  1247.     return ((Pop)q)->next;
  1248. }
  1249. static void *
  1250. do_preincr(Piv iv, unsigned char *p)
  1251. {
  1252. unsigned char *q = skip_bracket(p);
  1253.  
  1254.     p = POP->next;
  1255.     while(p < q)
  1256.     {
  1257.       p = do_something(iv, p);
  1258.     }
  1259.     return ((Pop)q)->next;
  1260. }
  1261. static void *
  1262. do_postincr(Piv iv, unsigned char *p)
  1263. {
  1264. unsigned char *q = skip_bracket(p);
  1265.  
  1266.     p = POP->next;
  1267.     while(p < q)
  1268.     {
  1269.       p = do_something(iv, p);
  1270.     }
  1271.     return ((Pop)q)->next;
  1272. }
  1273. static void *
  1274. do_compound(Piv iv, unsigned char *p)
  1275. {
  1276. unsigned char *q = skip_bracket(p);
  1277.  
  1278.     p = POP->next;
  1279.     while(p < q)
  1280.     {
  1281.       p = do_something(iv, p);
  1282.     }
  1283.     return ((Pop)q)->next;
  1284. }
  1285. static void *
  1286. do_unop(Piv iv, unsigned char *p)
  1287. {
  1288. unsigned char *q = skip_bracket(p);
  1289.  
  1290.     p = POP->next;
  1291.     while(p < q)
  1292.     {
  1293.       p = do_something(iv, p);
  1294.     }
  1295.     return ((Pop)q)->next;
  1296. }
  1297. static void *
  1298. do_expr(Piv iv, unsigned char *p)
  1299. {
  1300. unsigned char *q = skip_bracket(p);
  1301.  
  1302.     if(iv->debug >= '2')
  1303.         cfeprintf("EXPR inst(%u) `%s'\n", *p, oxgenops[*p]);
  1304.  
  1305.     while(p < q)
  1306.     {
  1307.         switch(*p)
  1308.         {
  1309.             case    arrayelemop:
  1310.                 p = do_arrayelem(iv, p);
  1311.                 break;
  1312.             case    funcallop:
  1313.                 p = do_funcall(iv, p);
  1314.                 break;
  1315.             case    condop:
  1316.                 p = do_cond(iv, p);
  1317.                 break;
  1318.             case    twopathop:
  1319.                 p = do_twopath(iv, p);
  1320.                 break;
  1321.             case    logicalop:
  1322.                 p = do_logical(iv, p);
  1323.                 break;
  1324.             case    binopop:
  1325.                 p = do_binop(iv, p);
  1326.                 break;
  1327.             case    strelemop:
  1328.                 p = do_strelem(iv, p);
  1329.                 break;
  1330.             case    ptrelemop:
  1331.                 p = do_ptrelem(iv, p);
  1332.                 break;
  1333.             case    argloadop:
  1334.                 p = do_argload(iv, p);
  1335.                 break;
  1336.             case    preincrdecop:
  1337.                 p = do_preincr(iv, p);
  1338.                 break;
  1339.             case    postincrdecop:
  1340.                 p = do_postincr(iv, p);
  1341.                 break;
  1342.             case    compoundop:
  1343.                 p = do_compound(iv, p);
  1344.                 break;
  1345.             case    unopop:
  1346.                 p = do_unop(iv, p);
  1347.                 break;
  1348.                 p = POP->next;
  1349.                 break;
  1350.             default:
  1351.                 p = do_something(iv, p);
  1352.                 break;
  1353.         }
  1354.     }
  1355.     return ((Pop)q)->next;
  1356. }
  1357. static void *
  1358. do_expstmt(Piv iv, unsigned char *p)
  1359. {
  1360. unsigned char *q = skip_bracket(p);
  1361.  
  1362.     p = POP->next;
  1363.     while(p < q)
  1364.     {
  1365.       p = do_something(iv, p);
  1366.     }
  1367.     return q;
  1368. }
  1369. static void *
  1370. do_ifstmt(Piv iv, unsigned char *p)
  1371. {
  1372. unsigned char *q = skip_bracket(p);
  1373.  
  1374.     p = POP->next;
  1375.     while(p < q)
  1376.     {
  1377.       p = do_something(iv, p);
  1378.     }
  1379.     return ((Pop)q)->next;
  1380. }
  1381. static void *
  1382. do_ifelsestmt(Piv iv, unsigned char *p)
  1383. {
  1384. unsigned char *q = skip_bracket(p);
  1385.  
  1386.     p = POP->next;
  1387.     while(p < q)
  1388.     {
  1389.       p = do_something(iv, p);
  1390.     }
  1391.     return ((Pop)q)->next;
  1392. }
  1393. static void *
  1394. do_switchstmt(Piv iv, unsigned char *p)
  1395. {
  1396. unsigned char *q = skip_bracket(p);
  1397.  
  1398.     p = POP->next;
  1399.     while(p < q)
  1400.     {
  1401.       p = do_something(iv, p);
  1402.     }
  1403.     return ((Pop)q)->next;
  1404. }
  1405. static void *
  1406. do_whilestmt(Piv iv, unsigned char *p)
  1407. {
  1408. unsigned char *q = skip_bracket(p);
  1409.  
  1410.     p = POP->next;
  1411.     while(p < q)
  1412.     {
  1413.       p = do_something(iv, p);
  1414.     }
  1415.     return ((Pop)q)->next;
  1416. }
  1417. static void *
  1418. do_dostmt(Piv iv, unsigned char *p)
  1419. {
  1420. unsigned char *q = skip_bracket(p);
  1421.  
  1422.     p = POP->next;
  1423.     while(p < q)
  1424.     {
  1425.       p = do_something(iv, p);
  1426.     }
  1427.     return ((Pop)q)->next;
  1428. }
  1429. static void *
  1430. do_forstmt(Piv iv, unsigned char *p)
  1431. {
  1432. unsigned char *q = skip_bracket(p);
  1433.  
  1434.     p = POP->next;
  1435.     while(p < q)
  1436.     {
  1437.       p = do_something(iv, p);
  1438.     }
  1439.     return ((Pop)q)->next;
  1440. }
  1441. static void *
  1442. do_asmstmt(Piv iv, unsigned char *p)
  1443. {
  1444. unsigned char *q = skip_bracket(p);
  1445.  
  1446.     p = POP->next;
  1447.     while(p < q)
  1448.     {
  1449.       p = do_something(iv, p);
  1450.     }
  1451.     return ((Pop)q)->next;
  1452. }
  1453. static void *
  1454. do_initstmt(Piv iv, unsigned char *p)
  1455. {
  1456. unsigned char *q = skip_bracket(p);
  1457.  
  1458.     p = POP->next;
  1459.     while(p < q)
  1460.     {
  1461.       p = do_something(iv, p);
  1462.     }
  1463.     return ((Pop)q)->next;
  1464. }
  1465. static void *
  1466. do_anfblock(Piv iv, unsigned char *p)
  1467. {
  1468. unsigned char *q = skip_bracket(p);
  1469.  
  1470.     p = POP->next;
  1471.     while(p < q)
  1472.     {
  1473.       p = do_something(iv, p);
  1474.     }
  1475.     return ((Pop)q)->next;
  1476. }
  1477. static void *
  1478. do_stmt(Piv iv, unsigned char *p)
  1479. {
  1480. void *q;
  1481.  
  1482.     if(iv->debug >= '2')
  1483.         cfeprintf("STMT inst(%u) `%s'\n", *p, oxgenops[*p]);
  1484.  
  1485.     q = POP->next;
  1486.     switch(*p)
  1487.     {
  1488.         case labelop:
  1489.             ENCODE(LABEL, dat: GL(POP->data));
  1490.             break;
  1491.         case gfuncdefop:
  1492.         case sfuncdefop:
  1493.         case funcexitop:
  1494.             PERROR(pName ":ERROR: Malformed input file1=%u=%p",*p, p);
  1495.             break;
  1496.         case nestedfuncdefop:
  1497.         {
  1498. #if 0
  1499.             if(iv->listing_wanted)
  1500.             {
  1501.                 obuf[0] = NFUNC;
  1502.                 *((char**)&obuf[1]) = iv->symaddr[GS(POPI->funcdef.symnum)];
  1503.                 link_ob(iv);
  1504.             }
  1505. #endif
  1506.             iv->numnested += 1;
  1507.             break;
  1508.         }
  1509.         case nestedfuncexitop:
  1510.         {
  1511.             break;        
  1512.         }
  1513.         case anfblockop:
  1514.             q = do_anfblock(iv, p);
  1515.             break;
  1516.         case expstmtop:
  1517.             q = do_expstmt(iv, p);
  1518.             break;
  1519.         case ifstmtop:
  1520.             q = do_ifstmt(iv, p);
  1521.             break;
  1522.         case ifelsestmtop:
  1523.             q = do_ifelsestmt(iv, p);
  1524.             break;
  1525.         case switchstmtop:
  1526.             q = do_switchstmt(iv, p);
  1527.             break;
  1528.         case whilestmtop:
  1529.             q = do_whilestmt(iv, p);
  1530.             break;
  1531.         case dostmtop:
  1532.             q = do_dostmt(iv, p);
  1533.             break;
  1534.         case forstmtop:
  1535.             q = do_forstmt(iv, p);
  1536.             break;
  1537.         case asmstmtop:
  1538.             q = do_asmstmt(iv, p);
  1539.             break;
  1540.         case initstmtop:
  1541.             q = do_initstmt(iv, p);
  1542.             break;
  1543.         case lineop:
  1544.             iv->lastline = GL(POP->data);
  1545.             ENCODE(LINE, dat: iv->lastline);
  1546.             break;
  1547.         default:
  1548.             break;
  1549.     }
  1550.     return q;
  1551. }
  1552. static void *
  1553. do_something(Piv iv, unsigned char *p)
  1554. {
  1555.  
  1556.     if(*p < labelop)
  1557.         return decode_anf(iv, p);
  1558.     else if(*p >= condop && *p <= unopop)
  1559.         return do_expr(iv, p);
  1560.     else
  1561.         return do_stmt(iv, p);
  1562. }
  1563. static void
  1564. do_bracket(Piv iv, unsigned char *p, unsigned char *q)
  1565. {
  1566.     p = POP->next;
  1567.     while(p < q)
  1568.     {
  1569.       p = do_something(iv, p);
  1570.     }
  1571. }
  1572. static void
  1573. dump_unix_codes(Piv iv)
  1574. {
  1575. PNODEC pcod = iv->cod_usedhead;
  1576.  
  1577.     while((pcod = pcod->next))
  1578.     {
  1579.     PNODEE p = &pcod->ee;
  1580.       if(p->inst)
  1581.       {
  1582.         if(p->inst > ENDCODES)
  1583.         {
  1584.             switch(p->inst)
  1585.             {
  1586.                 case    ALIGN:
  1587.                 {
  1588.                     DUMP("\t.align %d", p->size);
  1589.                     if(p->dat)
  1590.                         DUMP(",0x%lx", p->dat);
  1591.                     QDUMPC('\n');
  1592.                     break;
  1593.                 }
  1594.                 case    PUSHREGS:
  1595.                 {
  1596.                 int bits = p->dat;
  1597.                 int which = 31;
  1598.                     while(bits)
  1599.                     {
  1600.                         if(bits & 0x80000000)
  1601.                             DUMP("\tpushl\t%s\n", udatnames[which]);
  1602.                         bits <<= 1;
  1603.                         --which;
  1604.                     }
  1605.                     break;
  1606.                 }
  1607.                 case    POPREGS:
  1608.                 {
  1609.                 int bits = p->dat;
  1610.                 int which = 0;
  1611.                     while(bits)
  1612.                     {
  1613.                         if(bits & 1)
  1614.                             DUMP("\tpopl\t%s\n", udatnames[which]);
  1615.                         bits >>= 1;
  1616.                         ++which;
  1617.                     }
  1618.                     break;
  1619.                 }
  1620.                 case    LABEL:
  1621.                 {
  1622.                     DUMP("L%lu:\n", p->dat);
  1623.                     break;
  1624.                 }
  1625.                 case    LINE:
  1626.                 {
  1627.                     DUMP("//LINE:%lu\n", p->dat);
  1628.                     break;
  1629.                 }
  1630.                 case    FUNCNAME:
  1631.                 {
  1632.                     DUMP("_%s:\n", (char*)p->ptr);
  1633.                     break;
  1634.                 }
  1635.                 case LINEFEED:
  1636.                 {
  1637.                     QDUMPC('\n');
  1638.                     break;
  1639.                 }
  1640.                 default:
  1641.                     break;
  1642.             }
  1643.         }
  1644.         else
  1645.         {
  1646.             QDUMPC('\t');
  1647.             QDUMP(opnames[p->inst]);
  1648.             QDUMPC(uwordsize[p->size]);
  1649.             if(p->s1)
  1650.             {
  1651.                 QDUMPC('\t');
  1652.                 if(p->s1 == CONST)
  1653.                 {
  1654.                     DUMP("$%ld",p->dat);
  1655.                 }
  1656.                 else if(p->s1 == VARNAME)
  1657.                 {
  1658.                     QDUMPC('_');
  1659.                     QDUMP(p->ptr);
  1660.                 }
  1661.                 else if(p->s1 == LOCATION)
  1662.                 {
  1663.                     DUMP("L%lu", p->dat);
  1664.                 }
  1665.                 else
  1666.                 {
  1667.                     if(p->s1m & INDIRECT)
  1668.                         QDUMPC('*');
  1669.                     if(p->s1m & INDEXED)
  1670.                     {
  1671.                         if(p->dat)
  1672.                             DUMP("%ld(", p->dat);
  1673.                         else
  1674.                             QDUMPC('(');
  1675.                     }
  1676.                     QDUMP(udatnames[p->s1]);
  1677.                     if(p->s1m & INDEXED)
  1678.                         QDUMPC(')');
  1679.                 }
  1680.             }
  1681.             if(p->d1)
  1682.             {
  1683.                 if(p->s1)
  1684.                     QDUMPC(',');
  1685.                 else
  1686.                     QDUMPC('\t');
  1687.                 if(p->d1 == CONST)
  1688.                 {
  1689.                     DUMP("$%ld",p->dat);
  1690.                 }
  1691.                 else if(p->d1 == VARNAME)
  1692.                 {
  1693.                     QDUMPC('_');
  1694.                     QDUMP(p->ptr);
  1695.                 }
  1696.                 else
  1697.                 {
  1698.                     if(p->d1m & INDIRECT)
  1699.                         QDUMPC('*');
  1700.                     if(p->d1m & INDEXED)
  1701.                     {
  1702.                         if(p->dat)
  1703.                             DUMP("%ld(", p->dat);
  1704.                         else
  1705.                             QDUMPC('(');
  1706.                     }
  1707.                     QDUMP(udatnames[p->d1]);
  1708.                     if(p->d1m & INDEXED)
  1709.                         QDUMPC(')');
  1710.                 }
  1711.             }    
  1712.             QDUMPC('\n');
  1713.         } /* END: < ENDCODES */
  1714.       }/* END: if(p->inst) */
  1715.     }/* END: while(pcod->next) */
  1716. }
  1717. static void
  1718. start_unix_func(Piv iv, unsigned char *p)
  1719. {
  1720. char *funcname = iv->symaddr[GS(POPI->funcdef.symnum)];
  1721. int autosize = GL(POPI->funcdef.hidden_offset);
  1722. int tempmax = GS(POPI->funcdef.tempmax);
  1723. int stksiz = autosize + (tempmax*8);
  1724.  
  1725.     iv->regsave = 0;
  1726.     iv->botlabel = ++iv->lastlabel;
  1727.     iv->stksiz = stksiz;
  1728.  
  1729.     ENCODE(ALIGN, 2);
  1730.     ENCODE(FUNCNAME, ptr: funcname);
  1731.     ENCODE(push, LWORD, s1: ESP);
  1732.     ENCODE(mov, LWORD, s1: ESP, d1: EBP);
  1733.     ENCODE(PUSHREGS, LWORD);
  1734.     iv->regcode = iv->cod_usedtail;
  1735.     if(stksiz > 0)
  1736.         ENCODE(sub, LWORD, s1: CONST, d1: ESP, dat: stksiz);
  1737. }
  1738. static int
  1739. count_bits(int bits)
  1740. {
  1741. int cnt = 0;
  1742.     if(bits)
  1743.     {
  1744.         while(bits)
  1745.         {
  1746.             if(bits & 1)
  1747.                 ++cnt;
  1748.             bits >>= 1;
  1749.         }
  1750.     }
  1751.     return cnt;
  1752. }
  1753. static void
  1754. end_unix_func(Piv iv, unsigned char *p)
  1755. {
  1756.     ENCODE(LABEL, dat: iv->botlabel);
  1757.     if(iv->regsave)
  1758.     {
  1759.     int savcnt = count_bits(iv->regsave);
  1760.  
  1761.         iv->regcode->ee.dat = iv->regsave;
  1762.         if(iv->stksiz)
  1763.           ENCODE(lea, LWORD, s1: EBP, s1m: INDEXED, d1: ESP, dat: -(4*savcnt));
  1764.         ENCODE(POPREGS, LWORD, dat: iv->regsave);
  1765.     }
  1766.     ENCODE(leave);
  1767.     ENCODE(ret);
  1768.     ENCODE(LINEFEED);
  1769.  
  1770.     dump_unix_codes(iv);
  1771. }
  1772. static void
  1773. reset_funcdata(Piv iv)
  1774. {
  1775. #if 0
  1776.     iv->obuf = (char*)&iv->obufstart;
  1777.     iv->obufstart = 0;
  1778.     iv->obufcnt = 0;
  1779.     iv->func_offset = 0;
  1780.     iv->jbuf = (PJL)&iv->jbufstart;
  1781.     iv->jmpcnt = 0;
  1782.     iv->jbufcnt = 0;
  1783.     iv->jbufstart = 0;
  1784.     iv->extbuf = (PEL)&iv->extbufstart;
  1785.     iv->extcnt = 0;
  1786.     iv->extbufcnt = 0;
  1787.     iv->extbufstart = 0;
  1788.     iv->stackdepth = 0;
  1789.     iv->maxdepth = 0;
  1790.     iv->mindepth = 0;
  1791. #endif
  1792.     iv->numnested = 0;
  1793.     iv->cod_bufcnt = 0;
  1794.     iv->ob_bufcnt = 0;
  1795.     iv->cod_usedhead = 0;
  1796.     iv->ob_usedhead = 0;
  1797.     iv->first_cod = 0;
  1798.     iv->first_ob = 0;
  1799.     Cfreecat(FUNCDATA);
  1800. }
  1801. static void *
  1802. dumpa_unix_func(Piv iv, unsigned char *p)
  1803. {
  1804.     iv->ob = new_nodeO(iv);        /* setup first intermediate output node */
  1805.     link_ob(iv);                /* null node never unlinked */
  1806.     iv->first_ob = iv->ob_usedtail;
  1807.  
  1808.     iv->cod = new_nodeC(iv);    /* setup first code node */
  1809.     link_cod(iv);                /* null node never unlinked */
  1810.     iv->first_cod = iv->cod_usedtail;
  1811.  
  1812.     start_unix_func(iv, p);
  1813.     if((p = POP->next))
  1814.     {
  1815.         for(;;)
  1816.         {
  1817.             if(*p == funcexitop)
  1818.             {
  1819.                 end_unix_func(iv, p);
  1820.                 break;
  1821.             }
  1822.             else
  1823.                 p = do_something(iv, p);
  1824.         }
  1825.     }
  1826.     reset_funcdata(iv);
  1827.     return p;
  1828. }
  1829. static int
  1830. isa_reloc_loc(Piv iv, long offset, char **psymb, int *poffs)
  1831. {
  1832. long key[2];
  1833. struct _rval *vp;
  1834. struct _dval *dp;
  1835. unsigned char *p;
  1836. Pafile pf;
  1837.  
  1838.     key[0] = offset;
  1839.     key[1] = 0;
  1840.     if(SymFind(iv->newreloctbl, &key, &vp))
  1841.     {
  1842.         p = vp->p;
  1843.         pf = iv->files[vp->fileno];
  1844.         *poffs = vp->offset;
  1845.         if(*p == extlocop)
  1846.         {
  1847.             if(vp->rsym <= 0)
  1848.                 return 0;
  1849.             *psymb = pf->symaddr[vp->rsym];
  1850.             return 1;
  1851.         }
  1852.         else
  1853.         {
  1854.             key[0] = vp->base;
  1855.             if(SymFind(iv->datatbl, &key, &dp))
  1856.             {
  1857.                 p = dp->p;
  1858.                 switch(*p)
  1859.                 {
  1860.                     case    datablockop:
  1861.                     case    bssblockop:
  1862.                     case    thunkblockop:
  1863.                         *psymb = pf->symaddr[GS(POPI->s.symnum)];
  1864.                         return 1;
  1865.                     case    mallocblockop:
  1866.                     case    stringblockop:
  1867.                         *psymb = (void*)vp->base; /* must concoct a symbol */
  1868.                         return 2;
  1869.                 }
  1870.             }
  1871.             return 0;
  1872.         }
  1873.     }
  1874.     return 0;
  1875. }
  1876. static void
  1877. dump_unix_header(Piv iv)
  1878. {
  1879.     DUMP("\t.file\t\"%s\"\n_oxcc_compiled:\n\n",
  1880.         filenameof(iv->symaddr[1]));
  1881. }
  1882. static void
  1883. dump_unix_globals(Piv iv)
  1884. {
  1885.     if(SymHead(iv->gbltbl))
  1886.     {
  1887.         while(SymNext(iv->gbltbl))
  1888.         {
  1889.         struct _gloval *valp;
  1890.         unsigned char opcode;
  1891.  
  1892.             SymValue(iv->gbltbl, &valp);
  1893.             if((opcode = *(valp->p)))
  1894.             {
  1895.                 if(opcode == glodatop || opcode == glofuncop)
  1896.                 {
  1897.                     DUMP(".globl _%s\n", valp->symname);
  1898.                 }            
  1899.             }
  1900.         }
  1901.     }
  1902. }
  1903. static void
  1904. dump_unix_bss(Piv iv)
  1905. {
  1906.     if(SymHead(iv->datatbl))
  1907.     {
  1908.         while(SymNext(iv->datatbl))
  1909.         {
  1910.         unsigned char *p;
  1911.         long size;
  1912.         unsigned char opcode, prevopcode = 0;
  1913.         unsigned long *key;
  1914.         struct _dval *val;
  1915.             SymKey(iv->datatbl, &key);
  1916.             SymValue(iv->datatbl, &val);
  1917.             p = val->p;
  1918.             opcode = *p;
  1919.             if(val->prevp)
  1920.                 prevopcode = *(val->prevp);
  1921.             size = val->size;
  1922.             if(opcode == bssblockop)
  1923.             {
  1924.                 if(prevopcode == globssop)
  1925.                 {
  1926.                   DUMP(".comm _%s,%ld\n",
  1927.                       iv->symaddr[GS(POP->data2)], size);
  1928.                 }
  1929.                 else
  1930.                 {
  1931.                   DUMP(".lcomm _%s,%ld\n",
  1932.                       iv->symaddr[GS(POP->data2)], size);
  1933.                 }
  1934.             }
  1935.         }
  1936.     }
  1937. }
  1938. static int
  1939. dump_zero_bytes(Piv iv, unsigned char **pcp, int *pcnt, int cw)
  1940. {
  1941. unsigned char *cp = *pcp;
  1942.  
  1943.     if(*cp == 0)
  1944.     {
  1945.     int cnt;
  1946.     int zcnt = 0;
  1947.         cnt = *pcnt;
  1948.         while(*cp++ == 0 && cnt-- > 0)
  1949.             ++zcnt;
  1950.         if(zcnt > 3)
  1951.         {
  1952.             if(cw)
  1953.                 DUMP("\n  .space  %d", zcnt);
  1954.             else
  1955.                 DUMP("  .space  %d", zcnt);
  1956.             *pcp += zcnt;
  1957.             *pcnt -= zcnt;
  1958.             return 1;
  1959.         }
  1960.     }
  1961.     return 0;
  1962. }
  1963. static int
  1964. dump_zero_shorts(Piv iv, unsigned short **pcp, int *pcnt, int cw)
  1965. {
  1966. unsigned short *cp = *pcp;
  1967.  
  1968.     if(*cp == 0)
  1969.     {
  1970.     int cnt;
  1971.     int zcnt = 0;
  1972.         cnt = *pcnt;
  1973.         while(*cp++ == 0 && cnt-- > 0)
  1974.             ++zcnt;
  1975.         if(zcnt > 3)
  1976.         {
  1977.             if(cw)
  1978.                 DUMP("\n  .space  %d", zcnt*2);
  1979.             else
  1980.                 DUMP("  .space  %d", zcnt*2);
  1981.             *pcp += zcnt;
  1982.             *pcnt -= zcnt;
  1983.             return 1;
  1984.         }
  1985.     }
  1986.     return 0;
  1987. }
  1988. static int
  1989. dump_zero_longs(Piv iv, unsigned long **pcp, int *pcnt, int cw)
  1990. {
  1991. unsigned long *cp = *pcp;
  1992.  
  1993.     if(*cp == 0)
  1994.     {
  1995.     int cnt;
  1996.     int zcnt = 0;
  1997.         cnt = *pcnt;
  1998.         while(*cp++ == 0 && cnt-- > 0)
  1999.             ++zcnt;
  2000.         if(zcnt > 3)
  2001.         {
  2002.             if(cw)
  2003.                 DUMP("\n  .space  %d", zcnt*4);
  2004.             else
  2005.                 DUMP("  .space  %d", zcnt*4);
  2006.             *pcp += zcnt;
  2007.             *pcnt -= zcnt;
  2008.             return 1;
  2009.         }
  2010.     }
  2011.     return 0;
  2012. }
  2013. static void
  2014. dump_unix_bytes(Piv iv, unsigned char *cp, int cnt)
  2015. {
  2016. int first=0, width = 0;
  2017.     while(cnt > 0)
  2018.     {
  2019.         while(width <= 75 && cnt > 0)
  2020.         {
  2021.             if(dump_zero_bytes(iv, &cp, &cnt, width))
  2022.                 break;
  2023.             if(width == 0) {
  2024.                 width = QDUMP("  .byte  ");
  2025.                 first = 0;
  2026.             }
  2027.             if(!first++)
  2028.                 width += DUMP("0x%x", *cp++);
  2029.             else
  2030.                 width += DUMP(",0x%x", *cp++);
  2031.             --cnt;            
  2032.         }
  2033.         QDUMPC('\n');
  2034.         width = 0;
  2035.     }
  2036. }
  2037. static void
  2038. dump_unix_shorts(Piv iv, unsigned short *cp, int cnt)
  2039. {
  2040. int first=0, width = 0;
  2041.     while(cnt > 1)
  2042.     {
  2043.         while(width <= 73 && cnt > 0)
  2044.         {
  2045.             if(dump_zero_shorts(iv, &cp, &cnt, width))
  2046.                 break;
  2047.             if(width == 0) {
  2048.                 width = QDUMP("  .word  ");
  2049.                 first = 0;
  2050.             }
  2051.             if(!first++)
  2052.                 width += DUMP("0x%x", *cp++);
  2053.             else
  2054.                 width += DUMP(",0x%x", *cp++);
  2055.             cnt -= 2;            
  2056.         }
  2057.         QDUMPC('\n');
  2058.         width = 0;
  2059.     }
  2060.     if(cnt > 0)
  2061.         dump_unix_bytes(iv, (unsigned char *)cp, cnt);
  2062. }
  2063. static void
  2064. dump_unix_longs(Piv iv, unsigned long *cp, int cnt, long offset)
  2065. {
  2066. int first = 0, width = 0;
  2067. int rtyp;
  2068. char *psymb;
  2069. int extra;
  2070.  
  2071.     while(cnt > 3)
  2072.     {
  2073.         while(width <= 69 && cnt > 0)
  2074.         {
  2075.             if(dump_zero_longs(iv, &cp, &cnt, width))
  2076.                 break;
  2077.             if(width == 0) {
  2078.                 width = QDUMP("  .long  ");
  2079.                 first = 0;
  2080.             }
  2081.             if(!first++)
  2082.             {
  2083.                 if((rtyp = isa_reloc_loc(iv, offset, &psymb, &extra)))
  2084.                 {
  2085.                     if(rtyp == 1)
  2086.                     {
  2087.                         if(extra == 0)
  2088.                         {
  2089.                         width += DUMP("_%s", psymb);
  2090.                         }
  2091.                         else if(extra < 0)
  2092.                         {
  2093.                         width += DUMP("_%s%d", psymb,extra);
  2094.                         }
  2095.                         else
  2096.                         {
  2097.                         width += DUMP("_%s+%d", psymb,extra);
  2098.                         }
  2099.                     }
  2100.                     else
  2101.                     {
  2102.                         if(extra == 0)
  2103.                         {
  2104.                         width += DUMP("_$%p", psymb);
  2105.                         }
  2106.                         else if(extra < 0)
  2107.                         {
  2108.                         width += DUMP("_$%p%d", psymb,extra);
  2109.                         }
  2110.                         else
  2111.                         {
  2112.                         width += DUMP("_$%p+%d", psymb,extra);
  2113.                         }
  2114.                     }
  2115.                 }
  2116.                 else
  2117.                 {
  2118.                     width += DUMP("0x%lx", *cp++);
  2119.                 }
  2120.             }
  2121.             else
  2122.             {
  2123.                 if((rtyp = isa_reloc_loc(iv, offset, &psymb, &extra)))
  2124.                 {
  2125.                     if(rtyp == 1)
  2126.                     {
  2127.                         if(extra == 0)
  2128.                         {
  2129.                         width += DUMP(",_%s", psymb);
  2130.                         }
  2131.                         else if(extra < 0)
  2132.                         {
  2133.                         width += DUMP(",_%s%d", psymb,extra);
  2134.                         }
  2135.                         else
  2136.                         {
  2137.                         width += DUMP(",_%s+%d", psymb,extra);
  2138.                         }
  2139.                     }
  2140.                     else
  2141.                     {
  2142.                         if(extra == 0)
  2143.                         {
  2144.                         width += DUMP(",_$%p", psymb);
  2145.                         }
  2146.                         else if(extra < 0)
  2147.                         {
  2148.                         width += DUMP(",_$%p%d", psymb,extra);
  2149.                         }
  2150.                         else
  2151.                         {
  2152.                         width += DUMP(",_$%p+%d", psymb,extra);
  2153.                         }
  2154.                     }
  2155.                 }
  2156.                 else
  2157.                 {
  2158.                     width += DUMP(",0x%lx", *cp++);
  2159.                 }
  2160.             }
  2161.             offset += 4;
  2162.             cnt -= 4;            
  2163.         }
  2164.         QDUMPC('\n');
  2165.         width = 0;
  2166.     }
  2167.     if(cnt > 0)
  2168.         dump_unix_shorts(iv, (unsigned short*)cp, cnt);
  2169. }
  2170. static void
  2171. dump_unix_strings(Piv iv)
  2172. {
  2173.     QDUMP("\n.data\n");
  2174.     if(SymHead(iv->datatbl))
  2175.     {
  2176.         while(SymNext(iv->datatbl))
  2177.         {
  2178.         unsigned char *p;
  2179.         long size;
  2180.         unsigned char opcode;
  2181.         unsigned long *key;
  2182.         struct _dval *val;
  2183.  
  2184.             SymKey(iv->datatbl, &key);
  2185.             SymValue(iv->datatbl, &val);
  2186.             p = val->p;
  2187.             opcode = *p;
  2188.             size = val->size;
  2189.             if(opcode == stringblockop)
  2190.             {
  2191.                 DUMP("_$%lx:\n", GL(POP->data1));
  2192.                 dump_unix_bytes(iv, p+24, size);
  2193.             }
  2194.         }
  2195.     }
  2196. }
  2197. static void
  2198. dump_unix_data(Piv iv)
  2199. {
  2200. long mark[2];
  2201.  
  2202.     QDUMPC('\n');
  2203.     if(SymHead(iv->datatbl))
  2204.     {
  2205.         while(SymNext(iv->datatbl))
  2206.         {
  2207.         unsigned char *p;
  2208.         long size;
  2209.         unsigned char opcode;
  2210.         unsigned long *key;
  2211.         struct _dval *val;
  2212.         long offset;
  2213.  
  2214.             SymGetMark(iv->datatbl, mark);
  2215.             SymKey(iv->datatbl, &key);
  2216.             SymValue(iv->datatbl, &val);
  2217.             p = val->p;
  2218.             opcode = *p;
  2219.             size = val->size;
  2220.             offset = GL(POP->data1);
  2221.             if(opcode == datablockop)
  2222.             {
  2223.                 if(size > 3)
  2224.                   DUMP(".align 2\n_%s:\n",
  2225.                     iv->symaddr[GS(POP->data2)]);
  2226.                 else if(size > 1)
  2227.                   DUMP(".align 1\n_%s:\n",
  2228.                       iv->symaddr[GS(POP->data2)]);
  2229.                 dump_unix_longs(iv, (long*)(p+24), size, offset);
  2230.             }
  2231.             else if(opcode == mallocblockop)
  2232.             {
  2233.                 DUMP(".align 2\n_$%lx:\n", offset);
  2234.                 dump_unix_longs(iv, (long*)(p+24), size, offset);
  2235.             }
  2236.             SymSetMark(iv->datatbl, mark);
  2237.         }
  2238.     }
  2239. }
  2240. static void
  2241. dump_unix_funcs(Piv iv)
  2242. {
  2243. Pafile pf;
  2244. unsigned char *p;
  2245. int i;
  2246.  
  2247.     reset_funcdata(iv);
  2248.  
  2249.     QDUMP("\n\n.text\n");
  2250.     for(i = 0; i < iv->numfiles; ++i)
  2251.     {
  2252.         iv->filenum = i;
  2253.         pf = iv->files[i];
  2254.         p = pf->file_p;
  2255.  
  2256.         while(*p != endfileop)
  2257.         {
  2258.             if(*p == labelop)
  2259.             {
  2260. #if 0
  2261.                 DUMP("L%u:\n", GL(POP->data);
  2262. #endif
  2263.             }
  2264.             else if(*p == gfuncdefop || *p == sfuncdefop)
  2265.             {
  2266.                 p = dumpa_unix_func(iv, p);
  2267.             }
  2268.             else if(*p == anfblockop)
  2269.             {
  2270.                 PERROR(pName ": Sorry, Outer anf blocks not handled.\n");
  2271.             }
  2272.             p = POP->next;
  2273.         }
  2274.     }
  2275. }
  2276. static void
  2277. gen_unix_output(Piv iv)
  2278. {
  2279. long tim = time(0);
  2280. char *date = ctime(&tim);
  2281.  
  2282.     DUMP("// `%s'     %s// ", iv->outname, date);
  2283.     DUMP(notice,MAJOR_VERSION,MINOR_VERSION);
  2284.     QDUMP("\n\n");
  2285.  
  2286.     dump_unix_header(iv);
  2287.     dump_unix_globals(iv);
  2288.     dump_unix_strings(iv);
  2289.     dump_unix_data(iv);
  2290.     dump_unix_funcs(iv);
  2291.     dump_unix_bss(iv);
  2292. }
  2293. static void
  2294. gen_dos_output(Piv iv)
  2295. {
  2296. long tim = time(0);
  2297. char *date = ctime(&tim);
  2298.     DUMP(";  `%s'     %s\n;", iv->outname, date);
  2299.     DUMP(notice,MAJOR_VERSION,MINOR_VERSION);
  2300.     QDUMP("\n\n");
  2301. }
  2302. static int
  2303. gen_output(Piv iv, char *outpath)
  2304. {/* Assembler output */
  2305. char *cp;
  2306. Pafile pf;
  2307. int i;
  2308. char outname[256];
  2309.  
  2310.     strcpy(outname, outpath);
  2311.     pf = iv->files[0];
  2312.     iv->target_assembler = pf->header_p->hdr.target_assembler;
  2313.     iv->target_hardware = pf->header_p->hdr.target_hardware;
  2314.     iv->target_debugger = pf->header_p->hdr.target_debugger;
  2315.     iv->target_os = pf->header_p->hdr.target_os;
  2316.     iv->memory_model = pf->header_p->hdr.memory_model;
  2317.     iv->obj_format = pf->header_p->hdr.obj_format;
  2318.     if((cp = strrchr(outname, '.')))
  2319.     {
  2320. #if 0
  2321.         if(iv->target_assembler == UNIX || iv->target_assembler == GAS)
  2322.             strcpy(cp, ".s");
  2323.         else strcpy(cp, ".asm");
  2324. #endif
  2325.     }
  2326.     else
  2327.     {
  2328.         if(iv->target_assembler == UNIX || iv->target_assembler == GAS)
  2329.             strcat(outname, ".s");
  2330.         else
  2331.             strcat(outname, ".asm");
  2332.     }
  2333.     for(i = 1; i < iv->argc; ++i)
  2334.     {
  2335.       if(!strcmp(outname, iv->argv[i]))
  2336.       {
  2337.         PERROR(pName ":ERROR: output file `%s' is same as input file\n", outname);
  2338.       }
  2339.     }
  2340.     if(!(iv->outfile = fopen(outname, "wb")))
  2341.     {
  2342.         PERROR(pName ":ERROR: Cannot open output file %s\n", outname);
  2343.     }
  2344.     iv->outname = filenameof(outname);
  2345.  
  2346.     make_final_symtab(iv);
  2347.  
  2348.     if(iv->target_assembler == UNIX || iv->target_assembler == GAS)
  2349.         gen_unix_output(iv);
  2350.     else
  2351.         gen_dos_output(iv);
  2352.         
  2353.     fclose(iv->outfile);
  2354.     iv->outfile = 0;    
  2355.     return iv->errors;
  2356. }
  2357. /* ======================= END ASMCODE OUTPUT GENERATOR ==================== */
  2358.  
  2359. /* ===================== GENERIC CODE BELOW THIS POINT ================== */
  2360. static jmp_buf run_env;
  2361. static void
  2362. prerror(const char *fmt, ...)
  2363. {
  2364.     VFPRINTF(fmt, (char *)(((int *)(&fmt))+1));
  2365.     longjmp(run_env, 3);
  2366. }
  2367. static void
  2368. prwarn(const char *fmt, ...)
  2369. {
  2370.     VFPRINTF(fmt, (char *)(((int *)(&fmt))+1));
  2371. }
  2372. static void
  2373. info(const char *fmt, ...)
  2374. {
  2375.     vfprintf(stdout, fmt, (char *)(((int *)(&fmt))+1));
  2376. }
  2377. /* ========================= MULTI HEAP MALLOC ========================== */
  2378. #define LOCAL static
  2379.  
  2380. #if USING_FRAMEWORK
  2381. #define THEWELL(a) mallocC(local_category, a)
  2382. static int local_category;
  2383. static int num_instance;
  2384. extern void *mallocC(int, int);
  2385. extern void freecat(int);
  2386. extern void oxlink_clear_bss();
  2387. extern int NewMallocCategory();
  2388. #endif /* USING_FRAMEWORK */
  2389.  
  2390. #define BASE_CATEGORY 0
  2391. #define MEMORY_BUG 0
  2392. #define PRINT_RAWDATA 0
  2393.  
  2394. #if MEMORY_BUG == 1
  2395. #define MPRINTF printf
  2396. #else
  2397. #define MPRINTF(args...)
  2398. #endif
  2399.  
  2400. #define PAGESIZE (4096)    /* can use `pagesize' function in OS */
  2401. #define ALIGNMENTM (sizeof(unsigned long))
  2402. #define MAL_MAXLEVEL (12)
  2403. #define ROUNDINGM(a) ((a+(ALIGNMENTM-1))&~(ALIGNMENTM-1))
  2404. #define ALLOCSIZE (4096)
  2405. #define FRNTGUARD (0x544e5246UL)
  2406. #define BACKGUARD (0x48434142UL)
  2407. #ifndef THEWELL
  2408. #define THEWELL do_sbrk
  2409. #endif
  2410. #define NUMTYPES 3
  2411. #define SIZEH 0
  2412. #define FREEH 1
  2413. #define USEDH 2
  2414.  
  2415. #define SKIPVARS NodePM update[MAL_MAXLEVEL+1];NodePM node,prev;int level
  2416.  
  2417. #define DELETENODE(TYPE) \
  2418. {for(level=0;level<=bp->TYPE##level; level++)\
  2419. {if(update[level]->fptr[level] == node)\
  2420. update[level]->fptr[level] = node->fptr[level];else break;}\
  2421. while(bp->TYPE##level>0 && bp->TYPE##header->fptr[bp->TYPE##level]==_NILLL)\
  2422. bp->TYPE##level--;free_Mnode(bp,node,TYPE);}
  2423.  
  2424. #define INSERT() \
  2425. {while(level >= 0){\
  2426. node->fptr[level] = update[level]->fptr[level];\
  2427. update[level]->fptr[level] = node;level--;}}
  2428.  
  2429. #define SETLEVEL(TYPE) \
  2430. {level = getMlevel(bp, bp->TYPE##level);\
  2431. while(bp->TYPE##level < level)update[++bp->TYPE##level]=bp->TYPE##header;}
  2432.  
  2433. #define FINDKEY(TYPE, KEYVAL)\
  2434. {node = bp->TYPE##header;\
  2435. for(level = bp->TYPE##level; level >= 0; level--){\
  2436. while(node->fptr[level]->key < KEYVAL)\
  2437. node = node->fptr[level];\
  2438. update[level] = node;}prev=node;node=node->fptr[0];}
  2439.  
  2440. #define DETACH(SN)\
  2441. {SN->bptr->fptr=SN->fptr;if(SN->fptr)SN->fptr->bptr=SN->bptr;}
  2442.  
  2443. #define UNLINK(SN, N)\
  2444. {if(!sp->fptr&&sp->bptr->bptr<=(AddrP)(MAL_MAXLEVEL+1))dsize[N]=sp->size;\
  2445. DETACH(SN);free_addr(bp,SN);}
  2446.  
  2447. #define CHECKGUARDS(MSG)\
  2448. {if(bp->guarded){\
  2449. unsigned *p2;\
  2450. p2 = (void*)((char*)address+cursize-ALIGNMENTM);\
  2451. if(*address != FRNTGUARD)\
  2452. PERROR(pName #MSG ":%d: corrupted at 0x%x\n", bp->bincat, addr);\
  2453. if(*p2 != BACKGUARD)\
  2454. PERROR(pName #MSG ":%d: corrupted by 0x%x\n", bp->bincat, addr);}}
  2455.  
  2456. #if MEMORY_BUG == 1
  2457. #define HEAPCHECK \
  2458. {void *lastaddr;\
  2459. if(category > 0){\
  2460. Cguard(category);\
  2461. if((lastaddr = Cheapcheck(category, NULL))){\
  2462. FINDKEY(USEDH, (unsigned)lastaddr-ALIGNMENTM)\
  2463. MPRINTF("bad heap at %x c:%u size=%u\n", lastaddr, category, node->value);\
  2464. (void)print_rawdata(lastaddr-ALIGNMENTM, node->value);\
  2465. abort();}}}
  2466. #else
  2467. #define HEAPCHECK
  2468. #endif
  2469.  
  2470. struct _catlocs {
  2471.     void *addr;
  2472.     struct _catlocs *fptr;
  2473. };
  2474.  
  2475. typedef struct _nodeM
  2476. {
  2477.     unsigned key;
  2478.     unsigned value;
  2479.     unsigned levels;    /* must always be after value */
  2480.     struct _nodeM *fptr[1];
  2481. } NodeM, *NodePM;
  2482.  
  2483. typedef struct _addr
  2484. {
  2485.     struct _addr *fptr;
  2486.     struct _addr *bptr;
  2487.     NodePM maddr;
  2488.     unsigned size;
  2489. } *AddrP;
  2490.  
  2491. struct _bins {
  2492.     unsigned bits;
  2493.     unsigned nbits;
  2494.     NodePM SIZEHheader;
  2495.     int SIZEHlevel;
  2496.     NodePM FREEHheader;
  2497.     int FREEHlevel; 
  2498.     NodePM USEDHheader;
  2499.     int USEDHlevel;
  2500.  
  2501.     unsigned bincat;
  2502.     unsigned maxloc;
  2503.     unsigned minloc;
  2504.     struct _catlocs *catlocs;
  2505.     struct _bins *fptr;
  2506.     NodePM freenodes[NUMTYPES][MAL_MAXLEVEL+2];
  2507.     struct _addr *freeaddrlocs;
  2508.     char *chunkbase[NUMTYPES];
  2509.     int chunksize[NUMTYPES];
  2510.     int guarded;
  2511.     int addrbump;
  2512. };
  2513.  
  2514. static struct _bins zbp;
  2515. static struct _bins *hmap[1009];
  2516. static struct _nodeM _nilll = {0xffffffff,0,0,{0}};
  2517. static struct _nodeM *_NILLL = &_nilll;
  2518. static unsigned maxloc;
  2519. static unsigned minloc;
  2520. static struct _bins *freebinlocs;
  2521. static struct _catlocs *freecatlocs;
  2522. static char *binbase;
  2523. static int binsize;
  2524. static int chunksizes[] = {ALLOCSIZE,3*ALLOCSIZE,2*ALLOCSIZE};
  2525.  
  2526.  
  2527. static long randtbl[32]    = { 0L,
  2528.     0x9a319039L, 0x32d9c024L, 0x9b663182L, 0x5da1f342L, 
  2529.     0xde3b81e0L, 0xdf0a6fb5L, 0xf103bc02L, 0x48f340fbL, 
  2530.     0x7449e56bL, 0xbeb1dbb0L, 0xab5c5918L, 0x946554fdL, 
  2531.     0x8c2e680fL, 0xeb3d799fL, 0xb11ee0b7L, 0x2d436b86L, 
  2532.     0xda672e2aL, 0x1588ca88L, 0xe369735dL, 0x904f35f7L, 
  2533.     0xd7158fd6L, 0x6fa6f051L, 0x616e6b96L, 0xac94efdcL, 
  2534.     0x36413f93L, 0xc622c298L, 0xf5a42ab8L, 0x8a88d77bL, 
  2535.     0xf5ad9d0eL, 0x8999220bL, 0x27fb47b9L
  2536. };
  2537.  
  2538. static  long *fptr    = &randtbl[4];
  2539. static  long *rptr    = &randtbl[1];
  2540.  
  2541. /* ======================== START OF CODE =========================== */
  2542. #if PRINT_RAWDATA == 1
  2543. static char
  2544. hexbyte(unsigned int c)
  2545. {
  2546. char x = c & 0xf;
  2547.  
  2548.     return x + ((x>9) ? 55 : 48);
  2549. }
  2550. static void
  2551. print_rawdata(void *rawdata, long size)
  2552. {
  2553. unsigned long vaddr = 0;
  2554. unsigned char *d = rawdata;
  2555. int i,j;
  2556. char addr[9];
  2557. char hex1[24];
  2558. char hex2[24];
  2559. char side1[9];
  2560. char side2[9];
  2561.  
  2562.     addr[8] = 0;
  2563.     hex1[23] = 0;
  2564.     hex2[23] = 0;
  2565.     side1[8] = 0;
  2566.     side2[8] = 0;
  2567.     while(size > 0)
  2568.     {
  2569.     unsigned long qaddr = vaddr;
  2570.         memset(addr, '0', 8);
  2571.         memset(hex1, ' ', 23);
  2572.         memset(hex2, ' ', 23);
  2573.         memset(side1, ' ', 8);
  2574.         memset(side2, ' ', 8);
  2575.         i = 7;
  2576.         while(qaddr)
  2577.         {
  2578.             addr[i--] = hexbyte(qaddr);
  2579.             qaddr >>= 4;
  2580.         }
  2581.         for(i=0,j=0; i < 8; ++i)
  2582.         {
  2583.             if(--size >= 0)
  2584.             {
  2585.             unsigned int c = *d++;
  2586.                 if(isprint(c))
  2587.                     side1[i] = c;
  2588.                 else
  2589.                     side1[i] = '.';
  2590.                 hex1[j++] = hexbyte(c>>4);
  2591.                 hex1[j++] = hexbyte(c);
  2592.                     ++j;
  2593.             }
  2594.             else break;
  2595.         }
  2596.         for(i=0,j=0; i < 8; ++i)
  2597.         {
  2598.             if(--size >= 0)
  2599.             {
  2600.             unsigned int c = *d++;
  2601.                 if(isprint(c))
  2602.                     side2[i] = c;                    
  2603.                 else
  2604.                     side2[i] = '.';
  2605.                 hex2[j++] = hexbyte(c>>4);
  2606.                 hex2[j++] = hexbyte(c);
  2607.                 ++j;
  2608.             }
  2609.             else break;
  2610.         }
  2611.         VPRINTF("%s  %s%s%s  %s%s%s\n",addr,hex1," | ",hex2,side1,"|",side2);
  2612.         vaddr += 16;
  2613.     }
  2614. }
  2615. #endif
  2616.  
  2617. /*
  2618.  * Returns a really good 31-bit random number.
  2619.  */
  2620. static long
  2621. lrandom()
  2622. {
  2623. long i;
  2624.     
  2625.     *fptr += *rptr;
  2626.     i = (*fptr >> 1) & 0x7fffffffUL;
  2627.     if(++fptr > &randtbl[31])
  2628.     {
  2629.         fptr = &randtbl[1];
  2630.         ++rptr;
  2631.     }
  2632.     else
  2633.     {
  2634.         if(++rptr > &randtbl[31])  
  2635.             rptr = &randtbl[1];
  2636.     }
  2637.     return( i );
  2638. }
  2639. #if !USING_FRAMEWORK
  2640. static void *
  2641. do_sbrk(unsigned amount)
  2642. {
  2643. void *address;
  2644.  
  2645.     address = sbrk(amount);    /* OR WHATEVER TO ACCESS THE OPERATING SYSTEM */
  2646.     if(address == (void*)-1)
  2647.     {
  2648.         PERROR(pName "\nsystem out of memory, requested %u bytes\n", amount);
  2649.     }
  2650.     return address;
  2651. }
  2652. #endif
  2653.  
  2654. static struct _catlocs *
  2655. new_catloc(void)
  2656. {
  2657. struct _catlocs *p;
  2658.     if((p=freecatlocs))
  2659.     {
  2660.         freecatlocs = p->fptr;
  2661.         return p;
  2662.     }
  2663.     if(binsize < sizeof(struct _catlocs))
  2664.     {
  2665.         binbase = THEWELL(4096);
  2666.         binsize = 4096;
  2667.     }
  2668.     binsize -= sizeof(struct _catlocs);
  2669.     p = (void*)binbase;
  2670.     binbase += sizeof(struct _catlocs);
  2671.     return p;
  2672. }
  2673. static void
  2674. free_catloc(struct _catlocs *p)
  2675. {
  2676.     p->fptr = freecatlocs;
  2677.     freecatlocs = p;
  2678. }
  2679. static void *
  2680. new_chunk(struct _bins *bp, int size, int type)
  2681. {
  2682. char *p;
  2683.      if(bp->chunksize[type] < size)
  2684.     {
  2685.         if(bp->bincat == 0) {
  2686.             bp->chunkbase[type] = THEWELL(chunksizes[type]);
  2687.             bp->chunksize[type] = chunksizes[type];
  2688.         }
  2689.         else {
  2690.         struct _catlocs *cl;
  2691.             bp->chunkbase[type] = Cmalloc(0,chunksizes[type]-zbp.guarded);
  2692.             bp->chunksize[type] = chunksizes[type]-zbp.guarded;
  2693.             cl = new_catloc();
  2694.             cl->addr = bp->chunkbase[type];
  2695.             cl->fptr = bp->catlocs;
  2696.             bp->catlocs = cl;
  2697.         }
  2698.     }
  2699.     bp->chunksize[type] -= size;
  2700.     p = bp->chunkbase[type];
  2701.     bp->chunkbase[type] += size;
  2702.     return p;
  2703. }
  2704. static void *
  2705. new_Mnode(struct _bins *bp, int levels, int type)
  2706. {
  2707. int size;
  2708. NodePM p;
  2709.  
  2710.     if((p=bp->freenodes[type][levels]))
  2711.     {
  2712.         bp->freenodes[type][levels] = p->fptr[0];
  2713.         p->value = 0;
  2714.         return p;
  2715.     }
  2716.      size = sizeof(struct _nodeM) + levels * sizeof(void*);
  2717.     p = new_chunk(bp, size, type);
  2718.     p->levels = levels;
  2719.     p->value = 0;
  2720.     return p;    
  2721. }
  2722. static void
  2723. free_Mnode(struct _bins *bp, NodePM p, int type)
  2724. {
  2725.     p->fptr[0] = bp->freenodes[type][p->levels];
  2726.     bp->freenodes[type][p->levels] = p;
  2727. }
  2728. static struct _addr *
  2729. new_addr(struct _bins *bp)
  2730. {
  2731. struct _addr *p;
  2732.     if((p=bp->freeaddrlocs))
  2733.     {
  2734.         bp->freeaddrlocs = p->fptr;
  2735.         return p;
  2736.     }
  2737.     return new_chunk(bp, sizeof(struct _addr), FREEH);
  2738. }
  2739. static void
  2740. free_addr(struct _bins *bp, struct _addr *p)
  2741. {
  2742.     p->fptr = bp->freeaddrlocs;
  2743.     bp->freeaddrlocs = p;
  2744. }
  2745. static struct _bins *
  2746. new_bins(void)
  2747. {
  2748. struct _bins *p;
  2749.     if((p=freebinlocs))
  2750.     {
  2751.         freebinlocs = p->fptr;
  2752.         return p;
  2753.     }
  2754.      if(binsize < sizeof(struct _bins))
  2755.     {
  2756.         binbase = THEWELL(4096);
  2757.         binsize = 4096;
  2758.     }
  2759.     binsize -= sizeof(struct _bins);
  2760.     p = (struct _bins*)binbase;
  2761.     binbase += sizeof(struct _bins);
  2762.     return p;
  2763. }
  2764. static void
  2765. free_bins(struct _bins *p)
  2766. {
  2767.     p->fptr = freebinlocs;
  2768.     freebinlocs = p;
  2769. }
  2770. static int
  2771. getMlevel (struct _bins *p, int binlevel)
  2772. {
  2773. int level = -1;
  2774. int bits = 0;
  2775.  
  2776.     while(bits == 0)
  2777.     {
  2778.         if (p->nbits == 0)
  2779.         {
  2780.             p->bits = lrandom();
  2781.             p->nbits = 15;
  2782.         }
  2783.         bits = p->bits & 3;
  2784.         p->bits >>= 2;
  2785.         p->nbits--;
  2786.  
  2787.         if(++level > binlevel)
  2788.             break;
  2789.     }
  2790.     return (level > MAL_MAXLEVEL) ? MAL_MAXLEVEL : level;
  2791. }
  2792.  
  2793. static void
  2794. init_bins(struct _bins *bp, int category)
  2795. {
  2796. int i;
  2797. int binnum = category % 1009;
  2798.  
  2799.     bzero(bp, sizeof(struct _bins));
  2800.     bp->bincat = category;
  2801.     bp->minloc = 0xffffffff;
  2802.     bp->fptr = hmap[binnum];
  2803.     hmap[binnum] = bp;
  2804.     bp->SIZEHheader = new_Mnode(bp, MAL_MAXLEVEL+1, SIZEH);
  2805.     bp->FREEHheader = new_Mnode(bp, MAL_MAXLEVEL+1, FREEH);
  2806.     bp->USEDHheader = new_Mnode(bp, MAL_MAXLEVEL+1, USEDH);
  2807.  
  2808.     for(i = 0; i <= MAL_MAXLEVEL; ++i)
  2809.     {
  2810.         bp->SIZEHheader->fptr[i] = _NILLL;
  2811.         bp->FREEHheader->fptr[i] = _NILLL;
  2812.         bp->USEDHheader->fptr[i] = _NILLL;
  2813.     }
  2814. }
  2815.  
  2816. static struct _bins*
  2817. getcat(int category)
  2818. {
  2819. struct _bins *hbp;
  2820.  
  2821.     hbp = hmap[category % 1009];
  2822.     while(hbp)
  2823.     {
  2824.         if(hbp->bincat == category)
  2825.             return hbp;
  2826.         hbp = hbp->fptr;
  2827.     }
  2828.     return 0;
  2829. }
  2830. static struct _bins *
  2831. initcat(int category)
  2832. {
  2833. struct _bins *bp;
  2834.  
  2835.     if(category == 0)
  2836.     {
  2837.         bp = &zbp;
  2838.         if(zbp.SIZEHheader == 0)
  2839.             init_bins(bp, category);
  2840.         return bp;
  2841.     }
  2842.     /* do this to set zbp.guarded properly on startup */
  2843.     if(zbp.SIZEHheader == 0)
  2844.         initcat(0);
  2845.  
  2846.     if((bp = new_bins()))
  2847.     {
  2848.         init_bins(bp, category);
  2849.         return bp;
  2850.     }
  2851.     return 0;
  2852. }
  2853. static void *
  2854. getspace(struct _bins *bp, unsigned size, unsigned *remainder)
  2855. {
  2856. unsigned desired;
  2857. void *address;
  2858.   
  2859.     desired = ((size+ALLOCSIZE-1)/ALLOCSIZE)*ALLOCSIZE;
  2860.     if(bp->bincat == 0)
  2861.     {
  2862.         address = THEWELL(desired);
  2863.         *remainder = desired - size;
  2864.     }
  2865.     else
  2866.     {
  2867.     struct _catlocs *cl;
  2868.  
  2869.         if((desired-size) > zbp.guarded)
  2870.             desired -= zbp.guarded;
  2871.         
  2872.         address = Cmalloc(0, desired);
  2873.         *remainder = desired - size;
  2874.  
  2875.         /* save the gross allocations for the category */
  2876.         cl = new_catloc();
  2877.         cl->addr = address;
  2878.         cl->fptr = bp->catlocs;
  2879.         bp->catlocs = cl;
  2880.     }
  2881.     /* maintain address range info */
  2882.     if((unsigned)address < bp->minloc)
  2883.         bp->minloc = (unsigned)address;
  2884.     if(((unsigned)address + desired) > bp->maxloc)
  2885.         bp->maxloc = (unsigned)address + desired;
  2886.      if(bp->minloc < minloc)
  2887.          minloc = bp->minloc;
  2888.      if(bp->maxloc > maxloc)
  2889.          maxloc = bp->maxloc;
  2890.     return address;
  2891. }
  2892. static void
  2893. addto_sizelist(struct _bins *bp, AddrP ap)
  2894. {
  2895. SKIPVARS;
  2896.  
  2897.     /* INSERT IN SIZE LIST */
  2898.     FINDKEY(SIZEH, ap->size)
  2899.  
  2900.     if(node->key == ap->size)
  2901.     {/* size node exists */
  2902.         ap->fptr = (AddrP)node->value;
  2903.         ap->bptr = (AddrP)&node->value;
  2904.         if(ap->fptr) ap->fptr->bptr = ap;
  2905.         node->value = (unsigned)ap;
  2906.     }
  2907.     else
  2908.     {/* create new size node */
  2909.         SETLEVEL(SIZEH)
  2910.         node = new_Mnode(bp, level, SIZEH);
  2911.         node->key = ap->size;
  2912.         node->value = (unsigned)ap;
  2913.         ap->fptr = 0;
  2914.         ap->bptr = (AddrP)&node->value;
  2915.         INSERT()
  2916.     }
  2917. }
  2918. static void
  2919. addto_freelist(struct _bins *bp, void *addr, unsigned size)
  2920. {
  2921. SKIPVARS;
  2922. AddrP ap,sp;
  2923. unsigned dsize[2];
  2924.  
  2925.     /* GET NEW ADDR STRUCT */
  2926.     ap = new_addr(bp);
  2927.     ap->size = size;
  2928.  
  2929.     dsize[1] = dsize[0] = 0; /* sizenode deletion markers */
  2930.  
  2931.     /* CHECK FREE LIST */
  2932.     FINDKEY(FREEH, (unsigned)addr)
  2933.  
  2934.     /* CHECK FOR MERGE OR INSERT */
  2935.     if(prev->value && prev->key+((AddrP)prev->value)->size == (unsigned)addr)
  2936.     {/* merge with previous block */
  2937.         ap->size += ((AddrP)prev->value)->size;
  2938.  
  2939.         if(prev->key + ap->size == node->key)
  2940.         {/* merge with previous and next block */
  2941.             sp = (AddrP) node->value;;
  2942.             ap->size += sp->size;
  2943.  
  2944.             /* delete size struct for next block */
  2945.             UNLINK(sp, 0)
  2946.  
  2947.             /* delete next block */
  2948.             DELETENODE(FREEH);
  2949.         }
  2950.         /* delete size struct for prev block */
  2951.         sp = (AddrP)prev->value;
  2952.         UNLINK(sp, 1)
  2953.  
  2954.         /* set new address struct */
  2955.         prev->value = (unsigned)ap;
  2956.         ap->maddr = prev;
  2957.     }
  2958.     else if(node->value && (char*)addr + size == (void*)node->key)
  2959.     {/* merge with next block */
  2960.         sp = (AddrP) node->value;;
  2961.         node->key = (unsigned)addr;
  2962.         ap->size += sp->size;
  2963.  
  2964.         /* unlink size struct for next block */
  2965.         UNLINK(sp,0)
  2966.  
  2967.         /* set new address struct */
  2968.         node->value = (unsigned)ap;
  2969.         ap->maddr = node;
  2970.     }
  2971.     else
  2972.     {/* insert in free list */
  2973.  
  2974.         SETLEVEL(FREEH)
  2975.         node = new_Mnode(bp, level, FREEH);
  2976.         node->key = (unsigned)addr;
  2977.         node->value = (unsigned)ap;
  2978.         ap->maddr = node;
  2979.         INSERT()
  2980.     }
  2981.     addto_sizelist(bp, ap);
  2982.  
  2983.     /* Remove sizenodes eliminated by merge */
  2984.     if(dsize[0])
  2985.     {
  2986.         FINDKEY(SIZEH, dsize[0])
  2987.         if(node->value == 0)
  2988.           DELETENODE(SIZEH)
  2989.     }
  2990.     if(dsize[1])
  2991.     {
  2992.         FINDKEY(SIZEH, dsize[1])
  2993.         if(node->value == 0)
  2994.           DELETENODE(SIZEH)
  2995.     }
  2996. }
  2997.  
  2998. LOCAL void* 
  2999. Cmemalign(int category, unsigned alignment, unsigned req)
  3000. {
  3001. SKIPVARS;
  3002. NodePM fnode;
  3003. unsigned remainder;
  3004. unsigned *address;
  3005. struct _bins *bp;
  3006. unsigned mask, size;
  3007.  
  3008.  
  3009.     if(!(bp = getcat(category)))
  3010.       if(!(bp = initcat(category)))
  3011.         return 0;
  3012. HEAPCHECK
  3013.     if(req == 0)
  3014.         req = ALIGNMENTM;
  3015.     else
  3016.         req += ROUNDINGM(req);
  3017.     size = req += bp->guarded;
  3018.  
  3019.     if(alignment)
  3020.     {
  3021.         alignment += ROUNDINGM(alignment);
  3022.         if(alignment > ALIGNMENTM)
  3023.         {
  3024.             mask = alignment -1;
  3025.             size = req + alignment + bp->guarded;
  3026.         }
  3027.         else
  3028.         {
  3029.             alignment = 0;
  3030.         }
  3031.     }
  3032.  
  3033.     /* check sizelist for candidate */
  3034.     FINDKEY(SIZEH, size)
  3035.     fnode = node;
  3036. trynext:
  3037.     if(node->key != 0xffffffff)
  3038.     {/* found an appropriately sized block */
  3039.     AddrP sp = (AddrP)node->value;
  3040.  
  3041.         if(!sp && node == fnode)
  3042.         {
  3043.         NodePM q;
  3044.             q = node->fptr[0];
  3045.             DELETENODE(SIZEH)
  3046.             node = q;
  3047.             goto trynext;
  3048.         }
  3049.         if(!sp)
  3050.         {/* no available space at this size */
  3051.             node = node->fptr[0];
  3052.             goto trynext;
  3053.         }
  3054.  
  3055.         /* extract some space from this block */
  3056.         remainder = node->key - size;
  3057.         address = (void*)sp->maddr->key;
  3058.         sp->maddr->key += size;
  3059.         DETACH(sp);
  3060.  
  3061.         if(node->value == 0)
  3062.         {/* no more blocks of this size, delete sizenode */
  3063.             if(node != fnode)
  3064.               FINDKEY(SIZEH, size)
  3065.             DELETENODE(SIZEH)
  3066.         }
  3067.  
  3068.         if(remainder == 0)
  3069.         {/* no remaining space,the node in freelist is exhausted, delete it */
  3070.             FINDKEY(FREEH, sp->maddr->key)
  3071.             DELETENODE(FREEH)
  3072.             free_addr(bp, sp);
  3073.         }
  3074.         else
  3075.         {/* space remains in block, move it to new size loc */
  3076.             sp->size = remainder;
  3077.             addto_sizelist(bp, sp);
  3078.         }
  3079.     }
  3080.     else
  3081.     {
  3082.         address = getspace(bp, size, &remainder);
  3083.         if(remainder)
  3084.           addto_freelist(bp, ((char*)address)+size, remainder);
  3085.     }
  3086.     if(alignment)
  3087.     {
  3088.     unsigned diff;
  3089.         if((diff = (unsigned)address & mask))
  3090.         {/* move address forward */
  3091.         char *naddress;
  3092.         unsigned lose;
  3093.             lose = alignment - diff;
  3094.             naddress = (char*)address + lose;
  3095.             addto_freelist(bp, address, lose);
  3096.             address = (unsigned*)naddress;
  3097.         }
  3098.     }
  3099.     if(bp->guarded)
  3100.     {
  3101.       *address = FRNTGUARD;
  3102.       *((unsigned*)(((char*)address)+req-ALIGNMENTM)) = BACKGUARD;
  3103.  
  3104.     }
  3105.  
  3106.     FINDKEY(USEDH, (unsigned)address)
  3107.  
  3108.     if(node->key == (unsigned)address) {
  3109.       PERROR(pName ":ERROR:allocC:%d: bookkeeping nodes are corrupted at:0x%x\n",
  3110.           category, address);
  3111.     }
  3112.     SETLEVEL(USEDH)
  3113.     node = new_Mnode(bp, level, USEDH);
  3114.     node->key = (unsigned)address;
  3115.     node->value = req;
  3116.     INSERT()    
  3117.  
  3118.     return address+bp->addrbump;
  3119. }
  3120. LOCAL void*
  3121. Ccalloc(int category, unsigned cnt, unsigned elem_size)
  3122. {
  3123. unsigned size = cnt * elem_size;
  3124. void* buf;;
  3125.  
  3126.   if((buf = Cmalloc(category, size)))
  3127.       bzero(buf, size);
  3128.   return buf;
  3129. };
  3130. LOCAL void
  3131. Cfree(int category, void* addr)
  3132. {
  3133. unsigned cursize;
  3134. unsigned *address;
  3135. struct _bins *bp;
  3136. SKIPVARS;
  3137.     if(addr)
  3138.     {
  3139.         if(!(bp = getcat(category))) {
  3140.             PERROR(pName ":ERROR:Cfree:%d: non-existant category at:0x%x\n",category,addr);
  3141.         }
  3142. HEAPCHECK
  3143.         address = (void*) ((unsigned*)addr - bp->addrbump);
  3144.         FINDKEY(USEDH, (unsigned)address)
  3145.         if(node->key != (unsigned)address) {
  3146.           PERROR(pName ":ERROR:Cfree:%d: bogus address=0x%x\n", category, addr);
  3147.         }
  3148.         cursize = node->value;
  3149.         CHECKGUARDS(Cfree)
  3150.         DELETENODE(USEDH)
  3151.  
  3152.         addto_freelist(bp, address, cursize);
  3153.     }
  3154.     else PERROR(pName ":ERROR:Cfree:%d: null pointer\n", category);
  3155. }
  3156. LOCAL void* 
  3157. Crealloc(int category, void* addr, unsigned newsize)
  3158. {
  3159. SKIPVARS;
  3160. unsigned cursize;
  3161. unsigned *address;
  3162. struct _bins *bp;
  3163. NodePM onode;
  3164.  
  3165.     if(addr == 0) 
  3166.         return Cmalloc(category, newsize);
  3167.     else
  3168.     {
  3169.         if(!(bp = getcat(category))) {
  3170.            PERROR(pName ":ERROR:reallocC:%d: non-existant category at:%x\n",category,addr);
  3171.         }
  3172. HEAPCHECK 
  3173.         if(newsize == 0)
  3174.             newsize = ALIGNMENTM;
  3175.         else
  3176.             newsize += ROUNDINGM(newsize);
  3177.         newsize += bp->guarded;
  3178.  
  3179.         address = (void*)(((char*)addr)-(bp->guarded/2));
  3180.         FINDKEY(USEDH, (unsigned)address)
  3181.         if(node->key != (unsigned)address) {
  3182.           PERROR(pName ":ERROR:reallocC:%d: bogus address=0x%x\n", category, addr);
  3183.         }
  3184.         cursize = node->value;
  3185.         node->value = newsize;
  3186.         onode = node;
  3187.  
  3188.         CHECKGUARDS(reallocC)
  3189.  
  3190.         if(newsize == cursize)
  3191.             return addr;
  3192.         if(newsize > cursize)
  3193.         {/* check if block can be extended */
  3194.         void *taddr = ((char*)address) + cursize;
  3195.         unsigned extendsize = newsize-cursize;
  3196.  
  3197.             /* check freelist for an available block at the right address */
  3198.             FINDKEY(FREEH, (unsigned)taddr)
  3199.             if(node->key == (unsigned)taddr)
  3200.             {
  3201.             AddrP sp = (AddrP)node->value;
  3202.                 if(sp->size >= extendsize)
  3203.                 {/* BLOCK CAN BE EXTENDED INTERNALLY */
  3204.                     node->key += extendsize;
  3205.                     sp->size -= extendsize;
  3206.                     DETACH(sp)
  3207.                     if(sp->size == 0)
  3208.                     {/* the extension block is used up, delete this node */
  3209.                         free_addr(bp, sp);
  3210.                         DELETENODE(FREEH)
  3211.                     }
  3212.                     else
  3213.                     {/* shift the remainder in the sizelist */
  3214.                         addto_sizelist(bp, sp);
  3215.                     }
  3216.                     /* SUCCESS */
  3217.                     if(bp->guarded)
  3218.                     {
  3219.                         *((unsigned*)(((char*)address)+newsize-ALIGNMENTM))
  3220.                             = BACKGUARD;
  3221.                     }
  3222.                     return addr;
  3223.                 }
  3224.             }
  3225.             /* HERE WE COULD CHECK OTHER SOURCES OF SPACE */
  3226.  
  3227.             /* can't extend block, malloc some new space */
  3228.             if((taddr = Cmalloc(category,newsize-bp->guarded)))
  3229.             {
  3230.                 memmove(taddr,addr,cursize-bp->guarded);
  3231.                 onode->value = cursize;
  3232.                 Cfree(category, addr);
  3233.             }
  3234.             /* SUCCESS */
  3235.             return taddr;
  3236.         }
  3237.         else
  3238.         {/* shrink block */
  3239.             if(bp->guarded)
  3240.             {
  3241.                 *((unsigned*)(((char*)address)+newsize-ALIGNMENTM))
  3242.                     = BACKGUARD;
  3243.             }
  3244.             addto_freelist(bp, ((char*)address)+newsize, cursize-newsize); 
  3245.             return addr;
  3246.         }
  3247.       }
  3248. }
  3249. LOCAL void
  3250. Cfreecat(int category)
  3251. {
  3252. struct _bins *bp;
  3253.  
  3254.     if(category == 0)
  3255.         return;
  3256.  
  3257.     if((bp = getcat(category)))
  3258.     {
  3259.     struct _catlocs *cl = bp->catlocs;
  3260.     struct _bins *hbp;
  3261.     struct _bins *prev;
  3262.  
  3263.         while(cl)
  3264.         {/* Space allocated to the category is moved to category 0 */
  3265.         void *ql = cl->fptr;
  3266.  
  3267.             Cfree(0, cl->addr);
  3268.             free_catloc(cl);
  3269.             cl = ql;
  3270.         }
  3271.         /* space for the _bins struct is placed on a free list */
  3272.         hbp = hmap[category % 1009];
  3273.         prev = 0;
  3274.         while(hbp)
  3275.         {
  3276.             if(hbp->bincat == category)
  3277.             {
  3278.                 if(prev == 0)
  3279.                     hmap[category % 1009] = hbp->fptr;
  3280.                 else
  3281.                     prev->fptr = hbp->fptr;
  3282.                 free_bins(hbp);
  3283.                 return;
  3284.             }
  3285.             prev = hbp;
  3286.             hbp = hbp->fptr;
  3287.         }
  3288.     }
  3289. }
  3290. LOCAL int
  3291. Cmemrange(int category, unsigned *min, unsigned *max)
  3292. {
  3293. struct _bins *bp;
  3294.  
  3295.     if((bp = getcat(category)))
  3296.     {
  3297.         *min = bp->minloc;
  3298.         *max = bp->maxloc;
  3299.         return 1;
  3300.     }
  3301.     return 0;
  3302. }
  3303. LOCAL int
  3304. Cusedrange(int category, unsigned *min, unsigned *max)
  3305. {
  3306. struct _bins *bp;
  3307. NodePM node;
  3308. int level;
  3309.  
  3310.     if((bp = getcat(category)))
  3311.     {
  3312.         node = bp->USEDHheader;
  3313.         *min = node->fptr[0]->key;
  3314.         for(level = bp->USEDHlevel; level >= 0; level--)
  3315.           while(node->fptr[level]->key < 0xffffffff)
  3316.             node = node->fptr[level];
  3317.         *max = node->key;
  3318.         return 1;
  3319.     }
  3320.     return 0;
  3321. }
  3322. LOCAL void
  3323. Ctotrange(unsigned *min, unsigned *max)
  3324. {
  3325.     *min = minloc;
  3326.     *max = maxloc;
  3327. }
  3328. LOCAL void
  3329. Cguard(int category)
  3330. {
  3331. struct _bins *bp;
  3332.  
  3333.     if(!(bp = getcat(category)))
  3334.       if(!(bp = initcat(category)))
  3335.           return;
  3336.  
  3337.     if(!bp->guarded)
  3338.     {
  3339.         bp->guarded = 2*ALIGNMENTM;
  3340.         bp->addrbump = 1;
  3341.     }
  3342. }
  3343. LOCAL void*
  3344. Cheapcheck(int category, void *start)
  3345. {
  3346. struct _bins *bp;
  3347. NodePM node,prev;
  3348. unsigned *p1,*p2;
  3349.  
  3350.     if((bp = getcat(category)))
  3351.     {
  3352.         if(bp->guarded)
  3353.         {
  3354.             prev = 0;
  3355.             node = bp->USEDHheader;
  3356.             while(        (node = node->fptr[0]) != (NodePM)0xffffffff
  3357.                     &&    node->key != 0xffffffffUL)
  3358.             {
  3359.                 if((void*)node->key > start)
  3360.                 {
  3361.                     p1 = (unsigned*)node->key;
  3362.                     if(*p1 != FRNTGUARD)
  3363.                     {
  3364.                         if(prev)
  3365.                             return (char*)prev->key+ALIGNMENTM;
  3366.                         else
  3367.                             return (void*)1;
  3368.                     }
  3369.                     p2 = (unsigned*)(((char*)p1)+node->value-ALIGNMENTM);
  3370.                     if(*p2 != BACKGUARD)
  3371.                         return (char*)node->key+ALIGNMENTM;
  3372.                 }
  3373.                 prev = node;
  3374.             }
  3375.         }
  3376.     }
  3377.     return 0;
  3378. }
  3379. LOCAL void* 
  3380. Cmalloc(int category, unsigned size)
  3381. {
  3382.     return Cmemalign(category, 0, size);
  3383. }
  3384.  
  3385. LOCAL void* 
  3386. Cvalloc(int category, unsigned bytes)
  3387. {
  3388.   return Cmemalign (category, PAGESIZE, bytes);
  3389. }
  3390. LOCAL unsigned
  3391. Cmallocsize(int category, void* addr)
  3392. {
  3393. struct _bins *bp;
  3394. SKIPVARS;
  3395.  
  3396.     if(addr && (bp = getcat(category)))
  3397.     {
  3398.     unsigned address = (unsigned)((unsigned*)addr - bp->addrbump);
  3399.         FINDKEY(USEDH, address)
  3400.         if(node->key == address)
  3401.             return node->value - bp->guarded;
  3402.     }
  3403.     return 0;
  3404. }
  3405.  
  3406. LOCAL int
  3407. Cnewcat()
  3408. {
  3409. static unsigned int cat = BASE_CATEGORY;
  3410.     return ++cat;
  3411. }
  3412.  
  3413.  
  3414. /* ====================== END MULTI-HEAP MALLOC ============================ */
  3415.  
  3416. /* ====================== SYMBOL TABLE HANDLERS ============================ */
  3417.  
  3418. typedef struct _key
  3419. {
  3420.     unsigned long k[2];
  3421.     unsigned long hv;
  3422. } KEY, *KEYP;
  3423.  
  3424. typedef struct _nodeS
  3425. {/* 44 bytes -- adjust size to suit application */
  3426.     unsigned long value[5];    /* 16 bytes */
  3427.     unsigned long key[2];    /* 8 bytes */
  3428.     struct _nodeS *fptr[4];    /* 16 bytes */
  3429. } NodeS, *NodePS;
  3430.  
  3431. typedef struct _pbuf
  3432. {/* symbol table object */
  3433.     int    nbins;            /* number of bins in dictionary */
  3434.     int lastbin;        /* for seq access */
  3435.     NodePS lastptr;        /* ditto */
  3436.     int category;        /* heap number */
  3437.     char *chunkbase;    /* node allocation base */
  3438.     int chunksize;        /* number of bytes available in current chunk */
  3439.     NodePS freelist;    /* list of freed nodes for allocation */
  3440.     int level;            /* sorted level */
  3441.     int bits;            /* sorted bits */
  3442.     int bitcnt;            /* sorted bitcnt */
  3443.     NodePS header;        /* sorted header */
  3444.     NodePS bins[0];        /* bins if hashed dictionary */
  3445. } *PbufP;
  3446.  
  3447. #define SYM_MAXLEVEL 12
  3448. #define TBL ((PbufP)tbl)
  3449.  
  3450. static struct _nodeS _nnil = {{0,0,0,0},{0xffffffff,0xffffffff},{0,0,0,0}};
  3451. static struct _nodeS *_NNIL = &_nnil;
  3452.  
  3453. static int
  3454. getSlevel (PbufP tbl)
  3455. {
  3456. int level = -1;
  3457. int bits = 0;
  3458.  
  3459.     while (bits == 0)
  3460.     {
  3461.         if (tbl->bitcnt == 0)
  3462.         {
  3463.             tbl->bits = lrandom();
  3464.             tbl->bitcnt = 15;
  3465.         }
  3466.  
  3467.         bits = tbl->bits & 3;
  3468.         tbl->bits >>= 2;
  3469.         tbl->bitcnt--;
  3470.  
  3471.         if(++level > tbl->level)
  3472.             break;
  3473.     }
  3474.     return (level > SYM_MAXLEVEL) ? SYM_MAXLEVEL : level;
  3475.  
  3476. }
  3477.  
  3478. static void
  3479. hash(void *key, KEY *cat)
  3480. {
  3481.     cat->k[0] = ((unsigned long*)key)[0];
  3482.     cat->k[1] = ((unsigned long*)key)[1];
  3483.     cat->hv = ((cat->k[0] ^ cat->k[1]) * 1103515245UL) + 12345;
  3484. }
  3485. static void
  3486. sym_hash(unsigned long *key, char *symb)
  3487. {
  3488. int len = strlen(symb);
  3489. int i;
  3490.     for(i = 0; i < len; ++i)
  3491.       ((unsigned char *)key)[i & 7] ^= symb[i];
  3492.     key[0] = ((key[0] ^ key[1]) * 1103515245UL) + 12345;
  3493.     key[1] = len;
  3494. }
  3495. static void *
  3496. new_Snode(PbufP tbl, int levels)
  3497. {
  3498. NodePS p;
  3499. int size;
  3500.     if(levels <= 3)
  3501.     {
  3502.         if(tbl->freelist)
  3503.         {
  3504.             p = tbl->freelist;
  3505.             tbl->freelist = p->fptr[0];
  3506.             p->fptr[0] = 0;
  3507.             return p;
  3508.         }
  3509.     }
  3510.     size = sizeof(struct _nodeS) + ((levels-3) * sizeof(void*));
  3511.     if(tbl->chunksize < size)
  3512.     {
  3513.         tbl->chunkbase = Ccalloc(tbl->category, 1, 4080);
  3514.         tbl->chunksize = 4080;
  3515.     } 
  3516.     tbl->chunksize -= size;
  3517.     p = (NodePS)tbl->chunkbase;
  3518.     tbl->chunkbase += size;
  3519.     return p;
  3520. }
  3521. static void
  3522. free_Snode(PbufP tbl, NodePS node)
  3523. {
  3524.     bzero(node, sizeof(struct _nodeS));
  3525.     node->fptr[0] = tbl->freelist;
  3526.     tbl->freelist = node;
  3527. }
  3528.  
  3529. static void*
  3530. NewSymTable(int category, int nbins)
  3531. {
  3532. PbufP tbl;
  3533.  
  3534.     tbl = Ccalloc(category, 1, nbins*sizeof(NodePS) + sizeof(struct _pbuf));
  3535.     if(nbins == 0)
  3536.     {/* sorted dictionary */
  3537.     int i;
  3538.         tbl->header = new_Snode(tbl, SYM_MAXLEVEL+1);
  3539.         for(i = 0; i <= SYM_MAXLEVEL; ++i)
  3540.             tbl->header->fptr[i] = _NNIL;
  3541.     }
  3542.     tbl->nbins = nbins;
  3543.     tbl->category = category;
  3544.     return tbl;
  3545. }
  3546. static int
  3547. SymFind(void *tbl, void *key, void *result)
  3548. {
  3549. NodePS node;
  3550.  
  3551.     if(tbl && key)
  3552.     {
  3553.       if(TBL->nbins)
  3554.       {/* hashed dictionary */
  3555.       KEY cat;
  3556.  
  3557.         hash(key, &cat);
  3558.         if((node = TBL->bins[cat.hv % TBL->nbins]))
  3559.         {
  3560.             do {
  3561.                 if(        node->key[0] == cat.k[0]
  3562.                     &&    node->key[1] == cat.k[1])
  3563.                 {
  3564.                     if(result)
  3565.                       *((NodePS *)result) = node;
  3566.                     TBL->lastbin = cat.hv % TBL->nbins;
  3567.                     TBL->lastptr = node;
  3568.                     return 1;
  3569.                 }
  3570.              } while((node = node->fptr[0]));
  3571.         }
  3572.         return 0;
  3573.       }
  3574.       else
  3575.       {/* sorted dictionary */
  3576.       int level;
  3577.  
  3578.         node = TBL->header;
  3579.         for(level = TBL->level; level >= 0; level--)
  3580.         {
  3581.           while( KEYLT(node->fptr[level]->key, ((unsigned long*)key)) )
  3582.             node = node->fptr[level];
  3583.         }
  3584.         node = node->fptr[0];
  3585.  
  3586.         TBL->lastptr = node;
  3587.         if(result)
  3588.             *((NodePS *)result) = node;
  3589.         return (KEYEQ(node->key, ((unsigned long*)key))) ? 1 : 0;
  3590.       }
  3591.     }
  3592.     return -1;
  3593. }
  3594. static int
  3595. SymFindRange(void *tbl, void *key, void *result)
  3596. {/* assumes 4 byte key and value (the value can be bigger) */
  3597. NodePS node;
  3598.  
  3599.     if(tbl && key)
  3600.     {
  3601.       if(TBL->nbins)
  3602.       {/* hashed dictionary */
  3603.         return 0;
  3604.       }
  3605.       else
  3606.       {/* sorted dictionary */
  3607.       NodePS prev;
  3608.       int level;
  3609.  
  3610.         node = TBL->header;
  3611.         for(level = TBL->level; level >= 0; level--)
  3612.         {
  3613.           while ( node->fptr[level]->key[0] < ((unsigned long*)key)[0] )
  3614.             node = node->fptr[level];
  3615.         }
  3616.         prev = node;
  3617.         node = node->fptr[0];
  3618.  
  3619.         if( node->key[0] == ((unsigned long*)key)[0] )
  3620.         {
  3621.             TBL->lastptr = node;
  3622.             if(result)
  3623.                 *((NodePS *)result) = node;
  3624.             return 1;
  3625.         }        
  3626.         if( ((unsigned long*)key)[0] < prev->key[0]+prev->value[0] )
  3627.         {
  3628.             TBL->lastptr = prev;
  3629.             if(result)
  3630.                 *((NodePS *)result) = prev;
  3631.             return 1;
  3632.         }
  3633.         return 0;
  3634.       }
  3635.     }
  3636.     return -1;
  3637. }
  3638. static void *
  3639. SymInsert(void *tbl, void *key, void *value, int datsiz)
  3640. {
  3641. NodePS node;
  3642.  
  3643.     if(tbl && key)
  3644.     {
  3645.       if(TBL->nbins)
  3646.       {/* hashed dictionary */
  3647.       KEY cat;
  3648.       NodePS *binp;
  3649.         hash(key, &cat);
  3650.         node = new_Snode(tbl, 0);
  3651.         TBL->lastbin = cat.hv % TBL->nbins;
  3652.         TBL->lastptr = node;
  3653.         binp = &TBL->bins[TBL->lastbin];
  3654.         if(value && datsiz)
  3655.           memcpy(node, value, MIN(datsiz,16));
  3656.         node->key[0] = cat.k[0];
  3657.         node->key[1] = cat.k[1];
  3658.         node->fptr[0] = *binp;
  3659.         *binp = node;
  3660.         return node;
  3661.       }
  3662.       else
  3663.       {/* sorted dictionary */
  3664.       int level;
  3665.       NodePS update[SYM_MAXLEVEL+1];
  3666.  
  3667.         node = TBL->header;
  3668.         for (level = TBL->level; level >= 0; level--)
  3669.         {
  3670.           while ( KEYLT(node->fptr[level]->key,((unsigned long*)key)) )
  3671.             node = node->fptr[level];
  3672.           update[level] = node;
  3673.         }
  3674.  
  3675.         level = getSlevel(tbl);
  3676.  
  3677.         while(TBL->level < level)
  3678.             update[++TBL->level] = TBL->header;
  3679.  
  3680.         node = new_Snode(tbl, level);
  3681.  
  3682.         if(value && datsiz)
  3683.           memcpy(node, value, MIN(datsiz,16));
  3684.         node->key[0] = ((unsigned long*)key)[0];
  3685.         node->key[1] = ((unsigned long*)key)[1];
  3686.  
  3687.         while(level >= 0)
  3688.         {
  3689.             node->fptr[level] = update[level]->fptr[level];
  3690.             update[level]->fptr[level] = node;
  3691.             level--;
  3692.         }
  3693.         TBL->lastptr = node;
  3694.         return node;
  3695.      }
  3696.     }
  3697.     return 0;
  3698. }
  3699. static int
  3700. StringFind(void *tbl, char *string, void *result)
  3701. {
  3702. unsigned long key[2];
  3703. struct {
  3704.     char *symname;
  3705. } *valp;
  3706.  
  3707.     key[0] = 0;
  3708.     key[1] = 0;
  3709.     sym_hash(key, string);
  3710.  
  3711.     if(SymFind(tbl, key, &valp) == 1)
  3712.     {
  3713.     unsigned long *key1;
  3714.         do {
  3715.             if(!strcmp(string, valp->symname))
  3716.             {
  3717.                 if(result)
  3718.                     *((void**)result) = valp;
  3719.                 return 1;
  3720.             }
  3721.             /* Check duplicates */
  3722.             if(!SymNext(tbl))
  3723.                 break;
  3724.             SymKey(tbl, &key1);
  3725.             SymValue(tbl, &valp);
  3726.         } while(KEYEQ(key, key1));
  3727.     }
  3728.     return 0;
  3729. }
  3730. static int
  3731. StringInsert(void *tbl, char *string, void *result)
  3732. {
  3733. unsigned long key[2];
  3734. struct {
  3735.     char *symname;
  3736. } *valp;
  3737.  
  3738.     key[0] = 0;
  3739.     key[1] = 0;
  3740.     sym_hash(key, string);
  3741.     if(SymFind(tbl, key, &valp))
  3742.     {/* hash keys match */
  3743.     unsigned long *key1;
  3744.         do {
  3745.             if(!strcmp(string, valp->symname))
  3746.             {
  3747.                 if(result)
  3748.                     *((void**)result) = valp;
  3749.                 return 1;
  3750.             }
  3751.             /* Check duplicates */
  3752.             if(!SymNext(tbl))
  3753.                 break;
  3754.             SymKey(tbl, &key1);
  3755.             SymValue(tbl, &valp);
  3756.         } while(KEYEQ(key, key1));
  3757.     }
  3758.     /* NOMATCH */
  3759.     valp = SymInsert(tbl, key, &string, 4);
  3760.     if(result)
  3761.         *((void**)result) = valp;
  3762.     return 0;
  3763. }
  3764. static void
  3765. SymDelete(void *tbl, void *key)
  3766. {
  3767. NodePS node;
  3768.  
  3769.     if(tbl && key)
  3770.     {
  3771.       if(TBL->nbins)
  3772.       {/* hashed dictionary */
  3773.       KEY cat;
  3774.       NodePS *binp;
  3775.       NodePS prev = 0;
  3776.  
  3777.         hash(key, &cat);
  3778.         binp = &TBL->bins[cat.hv % TBL->nbins];
  3779.         if((node = *binp))
  3780.         {
  3781.             do {
  3782.                 if(        node->key[0] == cat.k[0]
  3783.                     &&    node->key[0] == cat.k[1])
  3784.                 {
  3785.                     if(prev)
  3786.                         prev->fptr[0] = node->fptr[0];
  3787.                     else
  3788.                         *binp = node->fptr[0];
  3789.  
  3790.                     free_Snode(tbl, node);
  3791.                     if(TBL->lastptr == node)
  3792.                     {
  3793.                         TBL->lastptr = 0;
  3794.                         TBL->lastbin = TBL->nbins;
  3795.                     }
  3796.                     return;
  3797.                 }
  3798.                 prev = node;
  3799.              } while((node = node->fptr[0]));
  3800.         }
  3801.       }
  3802.       else
  3803.       {/* sorted dictionary */
  3804.       int level;
  3805.       NodePS update[SYM_MAXLEVEL+1];
  3806.  
  3807.         node = TBL->header;
  3808.         for(level = TBL->level; level >= 0; level--)
  3809.         {
  3810.           while ( KEYLT(node->fptr[level]->key, ((unsigned long*)key)) )
  3811.             node = node->fptr[level];
  3812.           update[level] = node;
  3813.         }
  3814.         node = node->fptr[0];
  3815.  
  3816.         if( KEYEQ(node->key, ((unsigned long*)key)) )
  3817.         {
  3818.             for(level = 0; level <= TBL->level; level++)
  3819.             {
  3820.                 if (update[level]->fptr[level] == node)
  3821.                      update[level]->fptr[level] = node->fptr[level];
  3822.                 else break;
  3823.             }
  3824.  
  3825.             while((TBL->level > 0) && (TBL->header->fptr[TBL->level] == _NNIL))
  3826.                 TBL->level--;
  3827.  
  3828.             if(TBL->lastptr == node)
  3829.                 TBL->lastptr = 0;
  3830.             free_Snode(tbl, node);
  3831.         }
  3832.       }
  3833.     }
  3834. }
  3835. static int
  3836. SymHead(void *tbl)
  3837. {/* Set up for sequential access */
  3838. int nbins;
  3839.  
  3840.     if(tbl)
  3841.     {
  3842.       if((nbins = TBL->nbins))
  3843.       {/* hashed dictionary */
  3844.       NodePS node;
  3845.       int i;
  3846.         TBL->lastptr = 0;
  3847.         for(i = 0; i < nbins; ++i)
  3848.         {
  3849.             if( (node = TBL->bins[i]) != 0)
  3850.             {
  3851.                 TBL->lastbin = i;
  3852.                 return 1;
  3853.             }
  3854.         }
  3855.         TBL->lastbin = nbins;
  3856.         return 0; /* empty */
  3857.       }
  3858.       else
  3859.       {/* sorted dictionary */
  3860.         TBL->lastptr = TBL->header;
  3861.         return (TBL->lastptr->fptr[0] == _NNIL) ? 0 : 1;
  3862.       }
  3863.     }
  3864.     return 0;
  3865. }
  3866. static int
  3867. SymNext(void *tbl)
  3868. {/* Move to next sequential entry */
  3869. int nbins;
  3870.  
  3871.     if(tbl)
  3872.     {
  3873.       if((nbins = TBL->nbins))
  3874.       {/* hashed dictionary */
  3875.         if(TBL->lastptr && ((TBL->lastptr = TBL->lastptr->fptr[0])))
  3876.             return 1;
  3877.         else
  3878.         {
  3879.         int i;
  3880.             for(i = TBL->lastbin; i < nbins; ++i)
  3881.             {
  3882.                 if((TBL->lastptr = TBL->bins[i]) != 0)
  3883.                 {
  3884.                     TBL->lastbin = i+1;
  3885.                     return 1;
  3886.                 }
  3887.             }
  3888.             return 0;
  3889.         }
  3890.       }
  3891.       else
  3892.       {/* sorted dictionary */
  3893.         if(TBL->lastptr)
  3894.         {
  3895.             if(TBL->lastptr != _NNIL)
  3896.                 TBL->lastptr = TBL->lastptr->fptr[0];
  3897.             return (TBL->lastptr == _NNIL) ? 0 : 1;
  3898.         }
  3899.       }
  3900.     }
  3901.     return 0;
  3902. }
  3903. static void
  3904. SymGetMark(void *tbl, void *markptr)
  3905. {
  3906.     if(tbl && markptr)
  3907.     {
  3908.         ((long*)markptr)[0] = TBL->lastbin;
  3909.         ((long*)markptr)[1] = (long)TBL->lastptr;
  3910.     }
  3911. }
  3912. static int
  3913. SymMarkNext(void *tbl, void *mark)
  3914. {/* Mark current position, and move to next sequential entry */
  3915.     SymGetMark(tbl, mark);
  3916.     return SymNext(tbl);
  3917. }
  3918. static void
  3919. SymSetMark(void *tbl, void *markptr)
  3920. {
  3921.     if(tbl && markptr)
  3922.     {
  3923.         TBL->lastbin = ((long*)markptr)[0];
  3924.         TBL->lastptr = (NodePS)((long*)markptr)[1];
  3925.     }
  3926. }
  3927. static void
  3928. SymKey(void *tbl, void *keyptr)
  3929. {/* Retrieve key info pointer for current spot */
  3930.  
  3931.     if(tbl && keyptr && TBL->lastptr)
  3932.         *((unsigned long**)keyptr) = &TBL->lastptr->key[0];
  3933. }
  3934. static void
  3935. SymValue(void *tbl, void *datptr)
  3936. {/* Retrieve value pointer for current spot */
  3937.  
  3938.     if(tbl && datptr && TBL->lastptr)
  3939.         *((unsigned long**)datptr) = &TBL->lastptr->value[0];
  3940. }
  3941.  
  3942. /* ==================== END SYMBOL TABLE HANDLERS ========================== */
  3943.  
  3944. /* ========================== OPTIMIZATION ================================= */
  3945. static int
  3946. forward(unsigned char *p)
  3947. {
  3948. unsigned char *next;
  3949.  
  3950.     do {
  3951.         next = (void*)((Pop)p)->next;
  3952.         while(        *next == 0
  3953.                 ||    *next == lineop
  3954.                 ||    *next == labelop)
  3955.             next = (void*)((Pop)next)->next;
  3956.  
  3957.         if(*next == endop)
  3958.         {
  3959.             if(*p == *(next+8))
  3960.             {
  3961.                 *p = 0;
  3962.                 *next = 0;
  3963.                 return 1;
  3964.             }
  3965.             return 0;
  3966.         }
  3967.     } while(forward(next));
  3968.  
  3969.     return 0;
  3970. }
  3971. static void
  3972. eliminate_extraneous_infops(Piv iv, int level)
  3973. {
  3974. Pafile pf;
  3975. unsigned char *p;
  3976. int i;
  3977.     for(i = 0; i < iv->numfiles; ++i)
  3978.     {
  3979.         iv->filenum = i;
  3980.         pf = iv->files[i];
  3981.         if(!(p = pf->prog_p))
  3982.             continue;
  3983.         if(pf->header_p->hdr.opt_level >= level)
  3984.             continue;
  3985.         pf->header_p->hdr.opt_level = level;
  3986.         while(*p != endfileop)
  3987.         {
  3988.             switch(*p)
  3989.             {
  3990.                 case    unopop:
  3991.                 case    arrayelemop:
  3992.                 case    ptrelemop:
  3993.                 case    strelemop:
  3994.                 case    ptrdimsop:
  3995.                 case    arraydimsop:
  3996.                     forward(p);
  3997.                     break;
  3998.             }
  3999.             p = POP->next;
  4000.         }
  4001.     }
  4002. }
  4003. static void
  4004. clean_temps(Piv iv)
  4005. {
  4006. long *key;
  4007. long *val;
  4008. long hitemp = iv->first_temp & 0xffff0000;
  4009.  
  4010.     if(iv->temps_written == 0)
  4011.         return;
  4012.     if(SymHead(iv->tmptbl))
  4013.     {
  4014.         while(SymNext(iv->tmptbl))
  4015.         {
  4016.             SymKey(iv->tmptbl, &key);
  4017.  
  4018.             if((key[0] & 0xffff0000) == hitemp)
  4019.             {
  4020.             char *ptr;
  4021.             long saveit;
  4022.  
  4023.                 SymValue(iv->tmptbl, &val);
  4024.                 saveit = val[1];
  4025.                 ptr = (void*)val[0];
  4026.  
  4027.                 val[0] = 0;    /* allow reuse of this slot */
  4028.                 val[1] = 0;
  4029.  
  4030.                 while(ptr)
  4031.                 {
  4032.                 void *nptr = (void*)((PopT)ptr)->tmpnum;
  4033.                     ((PopT)ptr)->tmpnum = key[0];
  4034.  
  4035.                     if(!saveit)
  4036.                     {
  4037.                     unsigned char *p = ptr-8;                    
  4038.                     unsigned char op = *p;
  4039.  
  4040.                         *p = 0;                        
  4041.                         ++iv->killop;
  4042.                         if(op == duptmpop)
  4043.                         {/* special test for post increment */
  4044.                             p = POP->next;
  4045.                             p = POP->next;
  4046.                             if(*p == grabop)
  4047.                                 *p = 0;
  4048.                         }
  4049.                     }
  4050.                     ptr = nptr;
  4051.                 }
  4052.             }
  4053.         }
  4054.         if(!hitemp)
  4055.             iv->temps_written = 0;
  4056.     }
  4057. }
  4058.  
  4059. static void
  4060. read_temp(Piv iv, PopT ptr, unsigned long last)
  4061. {
  4062. unsigned long key[2];
  4063. long *result;
  4064.  
  4065.     if(last == ptr->tmpnum)
  4066.         return;
  4067.  
  4068.     key[0] = ptr->tmpnum;
  4069.     key[1] = 0;
  4070.  
  4071.     if(SymFind(iv->tmptbl, key, &result) == 1)
  4072.     {
  4073.         result[1] = 1;
  4074.     }
  4075.     else PERROR(pName ":SYSERROR: read temp %d not found\n", key[0]);
  4076. }
  4077. static int 
  4078. reading_self(unsigned char *p, long tmpnum)
  4079. {
  4080.     if((p[2]&0xe0) == OPTEMP || (p[2]&0xe0) == OPRET)
  4081.     {
  4082.           return 1;
  4083.     }
  4084.     if((p[3]&0xe0) == OPTEMP || (p[3]&0xe0) == OPRET)
  4085.     {
  4086.         return 1;
  4087.     }
  4088.     return 0;
  4089. }
  4090. static long
  4091. write_temp(Piv iv, PopT ptr, unsigned char opcode)
  4092. {
  4093. long key[2];
  4094. long val[2];
  4095. long *result;
  4096. long hitemp = ptr->tmpnum & 0xffff0000;
  4097.  
  4098.     if(ptr->atype & A_MEMADDR && opcode < duptmpop)
  4099.     {/* actually reading from this destination slot */
  4100.         read_temp(iv, ptr, 0);
  4101.         return 0;
  4102.     }
  4103.  
  4104.     if(hitemp > (iv->first_temp & 0xffff0000))
  4105.     {/* Inner block, CompoundExp or NestedFunc */
  4106.         iv->first_temp = hitemp + 1;
  4107.     }
  4108.     else if(hitemp < (iv->first_temp & 0xffff0000))
  4109.     {/* Exit inner block */
  4110.         if(!reading_self(((char*)ptr)-8, ptr->tmpnum))
  4111.         {
  4112.             while(hitemp < (iv->first_temp & 0xffff0000))
  4113.             {
  4114.                 clean_temps(iv);
  4115.                 iv->first_temp -= 0x00010000;
  4116.             }
  4117.         }
  4118.     }
  4119.     if(ptr->tmpnum == iv->first_temp)
  4120.     {
  4121.         if(!reading_self(((char*)ptr)-8, ptr->tmpnum))
  4122.             clean_temps(iv);
  4123.     }
  4124.     ++iv->temps_written;
  4125.     key[0] = ptr->tmpnum;
  4126.     key[1] = 0;
  4127.  
  4128.     if(SymFind(iv->tmptbl, key, &result) == 1)
  4129.     {
  4130.     PopT optr = (PopT)result[0];
  4131.         result[0] = (long)ptr;
  4132.         ptr->tmpnum = (long)optr;
  4133.     }
  4134.     else
  4135.     {
  4136.         val[0] = (long)ptr;
  4137.         val[1] = 0;
  4138.  
  4139.         SymInsert(iv->tmptbl, key, val, 8);
  4140.         ptr->tmpnum = 0;
  4141.     }
  4142.     return key[0];
  4143. }
  4144. static void
  4145. eliminate_unused_temps(Piv iv, int level)
  4146. {
  4147. Pafile pf;
  4148. unsigned char *p;
  4149. int i;
  4150. long last_write;
  4151.  
  4152.     iv->tmptbl = NewSymTable(iv->category, 111);
  4153.     for(i = 0; i < iv->numfiles; ++i)
  4154.     {
  4155.         iv->filenum = i;
  4156.         pf = iv->files[i];
  4157.         if(pf->header_p->hdr.opt_level >= level)
  4158.             continue;
  4159.         pf->header_p->hdr.opt_level = level;
  4160. rekill:
  4161.         if(!(p = pf->prog_p))
  4162.             continue;
  4163.         iv->first_temp = 1;
  4164.         iv->temps_written = 0;
  4165.         iv->killop = 0;
  4166.         while(*p != endfileop)
  4167.         {
  4168.             while(*p < labelop)
  4169.             {
  4170.                 if(*p == truthop)
  4171.                 {/* truthops of single chars are unnecessary */
  4172.                   if((p[2]&0xe0) == OPTEMP)
  4173.                   {
  4174.                     if(((PopT)(p+20))->dsize == 1)
  4175.                     {
  4176.                         if(((PopT)(p+8))->tmpnum == ((PopT)(p+20))->tmpnum)
  4177.                         {
  4178.                             *p = 0;
  4179.                             break;
  4180.                         }
  4181.                         else
  4182.                         {/* may be needed for code generation */
  4183.                             *p = aliastmpop;
  4184.                         }
  4185.                     }
  4186.                   }
  4187.                 }
  4188.                 if(*p)
  4189.                 {
  4190.                     if(*p == jmptrueop || *p == jmpfalseop)
  4191.                         read_temp(iv,(PopT)(p+4), 0);
  4192.                     if(*p == retdataop)
  4193.                         read_temp(iv, (PopT)p, 0);
  4194.                     else
  4195.                     {
  4196.                         last_write = 0;
  4197.                         if((p[1]&0xe0) == OPTEMP)
  4198.                             last_write = write_temp(iv, (PopT)(p+8), *p);
  4199.                         if((p[2]&0xe0) == OPTEMP || (p[2]&0xe0) == OPRET)
  4200.                             read_temp(iv, (PopT)((p+8+(p[1]&0x1f))), last_write);
  4201.                         if((p[3]&0xe0) == OPTEMP || (p[3]&0xe0) == OPRET)
  4202.                             read_temp(iv, (PopT)((p+8+(p[1]&0x1f))+(p[2]&0x1f)), last_write);
  4203.                     }
  4204.                 }
  4205.                 break;
  4206.             }
  4207.             p = POP->next;
  4208.         }
  4209.         do {
  4210.             clean_temps(iv);
  4211.             iv->first_temp -= 0x00010000;
  4212.         } while(iv->first_temp > 0);
  4213.         if(iv->killop)
  4214.         {
  4215.             goto rekill;
  4216.         }
  4217.     }
  4218. }
  4219. static void
  4220. retarget_jmps(Piv iv, int level)
  4221. {
  4222. Pafile pf;
  4223. unsigned char *p;
  4224. int i;
  4225.     for(i = 0; i < iv->numfiles; ++i)
  4226.     {
  4227.         iv->filenum = i;
  4228.         pf = iv->files[i];
  4229.         if(!(p = pf->prog_p))
  4230.             continue;
  4231.         if(pf->header_p->hdr.opt_level >= level)
  4232.             continue;
  4233.     }
  4234. }
  4235.  
  4236.  
  4237. static void
  4238. optimize(Piv iv)
  4239. {
  4240.     eliminate_extraneous_infops(iv, 50);
  4241.     eliminate_unused_temps(iv, 51);
  4242.     retarget_jmps(iv, 52);
  4243. }
  4244. /* ========================== END OPTIMIZATION ============================= */
  4245. /* ====================== BASIC INPUT FILE PROCESSING ====================== */
  4246. static long
  4247. label_insert(Piv iv, long label, int filenum, unsigned char *p)
  4248. {
  4249. long *result;
  4250. struct {
  4251.     long k1;
  4252.     long k2;
  4253. } key;
  4254.  
  4255. struct {
  4256.     long newlabel;
  4257. } val;
  4258.  
  4259.     key.k1 = label;
  4260.     key.k2 = filenum;
  4261.  
  4262.     /* check for duplicate label -- they happen */
  4263.     if(SymFind(iv->labeltbl, &key, &result) == 1)
  4264.     {
  4265.         if(*p == labelop)
  4266.             *p = 0; /* kill the instruction */
  4267.         return 0;
  4268.     }
  4269.  
  4270.     val.newlabel = ++iv->lastlabel;
  4271.     SymInsert(iv->labeltbl, &key, &val, 4);
  4272.  
  4273. #if REALLY_NEED_OFFSETS
  4274.     key.k1 = val.newlabel;
  4275.     val.newlabel = -1;
  4276.     SymInsert(iv->newlabeltbl, &key, &val, 4);
  4277. #endif
  4278.  
  4279.     return iv->lastlabel;
  4280. }
  4281. static long
  4282. label_find(Piv iv, long label, int filenum)
  4283. {
  4284. struct {
  4285.     long k1;
  4286.     long k2;
  4287. } key;
  4288.  
  4289. long *result;
  4290.  
  4291.     key.k1 = label;
  4292.     key.k2 = filenum;
  4293.  
  4294.     if(SymFind(iv->labeltbl, &key, &result) == 1)
  4295.         return *result;
  4296.     else
  4297.         return 0;
  4298. }
  4299. #if REALLY_NEED_OFFSETS
  4300. static void
  4301. newlabel_insert(Piv iv, long label)
  4302. {
  4303. struct {
  4304.     long k1;
  4305.     long k2;
  4306. } key;
  4307.  
  4308. long *result;
  4309.  
  4310.     key.k1 = label;
  4311.     key.k2 = iv->filenum;
  4312.     if(SymFind(iv->newlabeltbl, &key, &result) == 1)
  4313.     {
  4314.         *result = ++iv->labelnum;
  4315.     }
  4316.     else PERROR(pName ":SYSERROR: Label %d not found\n", label);
  4317. }
  4318. static long
  4319. newlabel_fix(Piv iv, long label)
  4320. {
  4321.     if(label)
  4322.     {
  4323.     struct {
  4324.         long k1;
  4325.         long k2;
  4326.     } key ;
  4327.  
  4328.     long *val;
  4329.  
  4330.         key.k1 = label;
  4331.         key.k2 = iv->filenum;
  4332.         if(SymFind(iv->newlabeltbl, &key, &val) == 1)
  4333.         {
  4334.             return val[0];
  4335.         }
  4336.     }
  4337.     return label;
  4338. }
  4339. #endif /* REALLY_NEED_OFFSETS */
  4340.  
  4341. static void
  4342. extern_insert(Piv iv, unsigned char *p, int filenum)
  4343. {
  4344. struct {
  4345.     short k1;
  4346.     short k2;
  4347.     long k3;
  4348. } key;
  4349. struct {
  4350.     unsigned char *p;
  4351. } val;
  4352.  
  4353.     key.k1 = GS(POPI->s.symnum);
  4354.     key.k2 = filenum;
  4355.     key.k3 = 0;
  4356.  
  4357.     val.p = p;
  4358.     SymInsert(iv->extrntbl, &key, &val, 4);
  4359. }
  4360. static void
  4361. reloc_insert(Piv iv, int fileno, unsigned char *p)
  4362. {
  4363. struct _rkey key;
  4364. struct _rval val;
  4365.  
  4366.     key.spot = GL(POPI->reloc.spot);    /* reloc target offset */
  4367.     key.fileno = fileno;            /* fileno */
  4368.     val.opcode = *p;                    /* opcode */
  4369.     val.rsize = GL(POPI->reloc.rsize);    /* reloc size */
  4370.     val.p = p;                            /* pointer to input buffer */
  4371.     val.base = GL(POPI->reloc.base);        /* base of data object pointed to */
  4372.     val.offset = GL(POPI->reloc.offset);    /* offset to be added to base */
  4373.     val.rsym = GS(POPI->reloc.rsym);        /* symbol number if external */
  4374.  
  4375.     SymInsert(iv->reloctbl, &key, &val, 18);
  4376. }
  4377. static void
  4378. data_insert(void *tbl, unsigned long offset,
  4379.             unsigned long size, void *p, void *prevp)
  4380. {
  4381. static long locid = 1;
  4382. struct _dkey key;
  4383. struct _dval val;
  4384.  
  4385. unsigned char opcode, prevopcode = 0;
  4386.  
  4387.     key.offset = offset;
  4388.     key.pad = 0;
  4389.  
  4390.     val.size = size;
  4391.     val.p = p;
  4392.     val.prevp = prevp;
  4393.     val.locid = 0;
  4394.     opcode = *((unsigned char *)p);
  4395.     if(prevp)
  4396.     {
  4397.         prevopcode = *((unsigned char*)prevp);
  4398.         if(        prevopcode != glodatop
  4399.             &&    prevopcode != glofuncop
  4400.             &&    prevopcode != extfuncop
  4401.             &&    prevopcode != globssop)
  4402.         {
  4403.             val.locid = locid++;
  4404.         }    
  4405.     }
  4406.     SymInsert(tbl, &key, &val, sizeof(val));
  4407. }
  4408.  
  4409. static void
  4410. global_insert(Piv iv, Pafile pf, unsigned char *p)
  4411. {
  4412. unsigned long key[2];
  4413. struct _gloval val;
  4414. PopI pp;
  4415. unsigned char opcode = *p;
  4416.  
  4417.     if(opcode == extvarop)
  4418.         pp = POPI;
  4419.     else
  4420.         pp = (PopI)(POP->next+8);
  4421.  
  4422.     key[0] = 0;
  4423.     key[1] = 0;
  4424.  
  4425.     val.symnum = GS(pp->s.symnum);
  4426.     val.symname = pf->symaddr[val.symnum];
  4427.     val.p = p;
  4428.     val.pf = pf;
  4429.     if(val.symnum < 0 || val.symnum >= pf->numsyms)
  4430.     {
  4431.         PERROR(pName ":SYSERROR: BAD SYMNUM=%d opcode=%d\n", val.symnum, opcode);
  4432.     }
  4433.     sym_hash(key, val.symname);
  4434.  
  4435.     /* Duplicate entries are allowed */
  4436.     SymInsert(iv->gbltbl, key, &val, sizeof(val));
  4437. }
  4438.  
  4439. static int
  4440. setup_nodelinks(Piv iv, char *infile_name, void *inbuf, int insize)
  4441. {
  4442. unsigned char *p = inbuf;
  4443. unsigned char *endbuf = inbuf+insize;
  4444. Pafile pf=0;
  4445. int lastline = 0;
  4446. unsigned char *funcp;
  4447. unsigned char *nfuncp;
  4448.  
  4449.     while(p < endbuf && *p != endallop)
  4450.     {
  4451.     unsigned char *q = p;
  4452.         if(iv->debug >= '3')
  4453.         {
  4454.             cfeprintf("OP(%d '%s' p=%p line=%d)\n", *p, oxgenops[*p], p, lastline);
  4455.         }
  4456.         switch(*p)
  4457.         {
  4458.             case headerop:
  4459.                 if(iv->numfiles >= 1024) {
  4460.                     PERROR(pName ": Sorry, too many files\n");
  4461.                 }
  4462.                 pf = iv->files[iv->numfiles] = 
  4463.                     Ccalloc(iv->category, 1, sizeof(struct _afile));
  4464.                 pf->filenum = iv->numfiles++;
  4465.                 pf->file_p = p;
  4466.                 pf->header_p = POPI;
  4467.                 if(iv->strip)
  4468.                 {/* Gonna strip declarations and line numbers */
  4469.                     pf->header_p->hdr.target_debugger = 0;
  4470.                 }
  4471.                 break;
  4472.  
  4473.             case dataop:
  4474.                 pf->size_p = POPI;
  4475.                 pf->thunk_offset = GL(POPI->dat.thunk_offset);
  4476.                 pf->bss_offset = GL(POPI->dat.bss_offset);
  4477.                 break;
  4478.             case gfuncdefop:
  4479.             case sfuncdefop:
  4480.                 if(pf->prog_p == 0)
  4481.                     pf->prog_p = p;
  4482.                 funcp = p;
  4483.                 break;
  4484.             case funcexitop:
  4485.                 PS(((PopI)(funcp+8))->funcdef.tempmax) = GL(POPI->funcexit.tempmax);
  4486.                 break;
  4487.             case nestedfuncdefop:
  4488.                 nfuncp = p;
  4489.                 break;
  4490.             case nestedfuncexitop:
  4491.                 PS(((PopI)(nfuncp+8))->funcdef.tempmax) = GL(POPI->funcexit.tempmax);
  4492.                 break;
  4493.             case segdefop:
  4494.                 if(pf->seg_p == 0)
  4495.                     pf->seg_p = p;
  4496.                 pf->numsegs += 1;
  4497.                 iv->numsegs += 1;
  4498.                 break;
  4499.             case lineop:
  4500.                 lastline = GL( POPI->line.line );
  4501.                 if(iv->strip)
  4502.                     *p = 0;        /* strip line numbers */
  4503.                 break;
  4504.             case declop:
  4505.                 if(iv->strip)
  4506.                 {/* strip declarations */
  4507.                     do {
  4508.                         *p = 0;
  4509.                         q += (long)GL(POP->next);
  4510.                         POP->next = q;
  4511.                         p = q;
  4512.                     } while(*p != endop);                    
  4513.                     *p = 0;
  4514.                 }
  4515.                 else
  4516.                 {
  4517.                     if(pf->decl_p == 0)
  4518.                         pf->decl_p = p;
  4519.                     pf->numdecls += 1;
  4520.                     iv->numdecls += 1;
  4521.                 }
  4522.                 break;
  4523.             case switchidop:
  4524.                 if(pf->switch_p == 0)
  4525.                     pf->switch_p = p;
  4526.                 break;
  4527.             case labelop:
  4528.                 PL( POP->data ) = 
  4529.                         label_insert(iv, GL( POP->data ), pf->filenum, p);
  4530.                 break;            
  4531.             case symbop:
  4532.                 pf->numsyms = GL(POP->data);
  4533.                 iv->numsyms += pf->numsyms;
  4534.                 break;
  4535.             case symblockop:
  4536.                 pf->symtext_p = p + 12;
  4537.                 goto blka;
  4538.             case stringblockop:
  4539.             case datablockop:
  4540.             case mallocblockop:
  4541.             case thunkblockop:
  4542.             {
  4543.             long size;
  4544.                 if(pf->data_p == 0)
  4545.                     pf->data_p = p;
  4546. blka:
  4547.                 size = GL(POP->data);
  4548.                 q += size+((4-(size&3))&3);
  4549.                 break;
  4550.             }
  4551.             case glofuncop:
  4552.             case extfuncop:
  4553.             case glodatop:
  4554.             case globssop:
  4555.             case extvarop:
  4556.             case bssblockop:
  4557.                 if(pf->data_p == 0)
  4558.                     pf->data_p = p;
  4559.                 break;
  4560.             case maxtempop:
  4561.                 pf->maxtemp = GL(POP->data);
  4562.                 pf->maxtempclass = GL(POP->data1);
  4563.                 pf->maxtemp_p = p;
  4564.                 break;
  4565.         }
  4566.         q += (long)GL(POP->next);
  4567.         POP->next = q;
  4568.         p = q;
  4569.     }
  4570.     if(*p != endallop)
  4571.     {
  4572.         PERROR(pName ":ERROR: Malformed input file: %s\n", infile_name);
  4573.     }
  4574.     return 0;
  4575. }
  4576. static void
  4577. setup_syms_decls(Piv iv)
  4578. {
  4579. int i;
  4580.  
  4581.     for(i = 0; i < iv->numfiles; ++i)
  4582.     {
  4583.     int symnum = 0;
  4584.     Pafile pf = iv->files[i];
  4585.     unsigned char *p = pf->file_p;
  4586.     unsigned char *prevp = 0;
  4587.  
  4588.         pf->symaddr = Ccalloc(iv->category, sizeof(void*), pf->numsyms);
  4589.         pf->decladdr = Ccalloc(iv->category, sizeof(void*), pf->numdecls);
  4590.  
  4591.         while(*p != endfileop)
  4592.         {
  4593.             switch(*p)
  4594.             {
  4595.                 case    symoffsetop:
  4596.                   pf->symaddr[symnum] = pf->symtext_p + GL(POP->data);
  4597.                   ++symnum;
  4598.                   break;
  4599.  
  4600.                 case    declop:
  4601.                   pf->decladdr[GS(POPI->dcl.declnum)] = p;
  4602.                   break;
  4603.  
  4604.                 case    relocop:
  4605.                 case    extlocop:
  4606.                   ++pf->numrelocs;
  4607.                   reloc_insert(iv, i, p);
  4608.                   break;
  4609.  
  4610.                 case    glodatop:
  4611.                 case    globssop:
  4612.                 case    glofuncop:
  4613.                 case    extfuncop:
  4614.                   global_insert(iv, pf, p);
  4615.                   break;
  4616.  
  4617.                 case    extvarop:
  4618.                   extern_insert(iv, p, i);
  4619.                   global_insert(iv, pf, p);
  4620.                   break;
  4621.  
  4622.                 case    stringblockop:
  4623.                 case    datablockop:
  4624.                 case    mallocblockop:
  4625.                 case    thunkblockop:
  4626.                 case    bssblockop:
  4627.                   if(!pf->datatbl)
  4628.                     pf->datatbl = NewSymTable(iv->category, 0);  /* sorted */
  4629.  
  4630.                   data_insert(pf->datatbl,GL(DATI.offset),GL(DATI.size),
  4631.                               p, prevp);
  4632.                   if(*p == thunkblockop) {
  4633.                     PL(POP->data5) = label_find(iv, GL(POP->data5), i);
  4634.                   }
  4635.                   break;
  4636.  
  4637.                 case    jmploopop:
  4638.                 case    jmpcontinueop:
  4639.                 case    jmpbreakop:
  4640.                 case    jmpgotoop:
  4641.                 case    jmptrueop:
  4642.                 case    jmpfalseop:
  4643.                 case    funcstartop:
  4644.                 case    funcstopop:
  4645.                 case    casevalop:
  4646.                 case    switchop:
  4647.                   PL(POP->data) = label_find(iv, GL(POP->data), i);
  4648.                   break;
  4649.             }
  4650.             prevp = p;
  4651.             p = POP->next;
  4652.         }
  4653.     }
  4654. }
  4655. static int
  4656. sym_insert(Piv iv, char *symname, int symnum)
  4657. {/* Used only for combining symbols in link phase */
  4658. struct {
  4659.     char *symname;
  4660.     int symnum;
  4661. } *valp;
  4662.  
  4663.     if(StringInsert(iv->symtbl, symname, &valp))
  4664.         return -valp->symnum;    /* MATCH */
  4665.     valp->symnum = symnum;
  4666.     return symnum;
  4667. }
  4668. static void
  4669. combine_syms_decls(Piv iv)
  4670. {
  4671. int i,j;
  4672. Pafile pf;
  4673. int numsyms;
  4674. int numdecls;
  4675.  
  4676.     /* COMBINE SYMBOLS */
  4677.     pf = iv->files[0];
  4678.     numsyms = pf->numsyms;
  4679.     pf->symtran = Ccalloc(iv->category, sizeof(short), pf->numsyms);
  4680.     memcpy(iv->symaddr, pf->symaddr, sizeof(void*) * numsyms);
  4681.  
  4682.  
  4683.     for(i = 0; i < numsyms; ++i)
  4684.     {/* file 0 */
  4685.         sym_insert(iv, pf->symaddr[i], i);
  4686.         pf->symtran[i] = i;
  4687.     }
  4688.     for(i = 1; i < iv->numfiles; ++i)
  4689.     {
  4690.     int start;
  4691.  
  4692.         pf = iv->files[i];
  4693.         pf->symtran = Ccalloc(iv->category, sizeof(short), pf->numsyms);
  4694.         if(pf->header_p->hdr.target_debugger)
  4695.             start = 1;
  4696.         else
  4697.             start = 3;
  4698.         for(j = start; j < pf->numsyms; ++j)
  4699.         {
  4700.         int k;
  4701.           if((k = sym_insert(iv, pf->symaddr[j], numsyms)) > 0)
  4702.           { /* new entry */
  4703.             iv->symaddr[numsyms++] = pf->symaddr[j];
  4704.             pf->symtran[j] = k;
  4705.           }
  4706.           else pf->symtran[j] = -k;
  4707.         }
  4708.     }
  4709.     iv->numsyms = numsyms;
  4710.  
  4711.     /* COMBINE DECLARATIONS */
  4712.     pf = iv->files[0];
  4713.     numdecls = pf->numdecls;
  4714.     pf->decltran = Ccalloc(iv->category, sizeof(short), pf->numdecls);
  4715.     memcpy(iv->decladdr, pf->decladdr, sizeof(void*) * numdecls);
  4716.  
  4717.     for(i = 0; i < numdecls; ++i)
  4718.     {/* file 0 */
  4719.         pf->decltran[i] = i;
  4720.     }
  4721.     for(i = 1; i < iv->numfiles; ++i)
  4722.     {
  4723.         pf = iv->files[i];
  4724.         pf->decltran = Ccalloc(iv->category, sizeof(short), pf->numdecls);
  4725.         if(pf->numdecls < 21)
  4726.             continue;
  4727.         for(j = 1; j <= 21; ++j)
  4728.             pf->decltran[j] = j;
  4729.         for(j = 22; j < pf->numdecls; ++j) {
  4730.             iv->decladdr[numdecls] = pf->decladdr[j];
  4731.             pf->decltran[j] = numdecls++;
  4732.         }
  4733.     }
  4734.     iv->numdecls = numdecls;
  4735. }
  4736.  
  4737. static void
  4738. link_dups(Piv iv, int dupcnt, struct _gloval *valp[])
  4739. {
  4740. int i;
  4741. int vars[5] = {0,0,0,0,0};
  4742. unsigned long cdsize = 0;
  4743. unsigned long cdoffset = 0;
  4744. short cdfile = 0;
  4745. int cdnum = 0;
  4746. short segid = 0;
  4747.  
  4748. #define GDAT vars[0]
  4749. #define GBSS vars[1]
  4750. #define GFUNC vars[2]
  4751. #define EVAR vars[3]
  4752. #define EFUNC vars[4]
  4753.  
  4754.     /* Count the types of matches */
  4755.     for(i = 0; i <= dupcnt; ++i)
  4756.         vars[*(valp[i]->p) - glodatop] += 1;
  4757.  
  4758.     /* Check for errors */
  4759.     if(        GDAT > 1 
  4760.         ||    GFUNC > 1
  4761.         ||    (GFUNC && (GDAT || GBSS || EVAR))
  4762.         ||    (EFUNC && (GDAT || GBSS || EVAR)))
  4763.     {
  4764.         ++iv->errors;
  4765.         for(i = 0; i < dupcnt; ++i)
  4766.         {
  4767.             PWARN(pName ": Symbol `%s' multiply defined or mistyped.\n",
  4768.                   valp[i]->symname);
  4769.             PWARN(pName":  In file: `%s'\n", valp[i]->pf->symaddr[INFILE_SYMNUM]);
  4770.         }
  4771.         return;
  4772.     }
  4773.     if(EFUNC && GFUNC)
  4774.     {/* match up functions */
  4775.     Pop dp;
  4776.         for(i = 0; i <= dupcnt; ++i)
  4777.           if(*(valp[i]->p) == glofuncop)
  4778.             break;
  4779.         dp = (Pop)((Pop)valp[i]->p)->next;    /* points to thunkblockop */
  4780.  
  4781.         cdoffset = GL(dp->data1);            /* save this offset */
  4782.         cdfile = valp[i]->pf->filenum;        /* save this file */
  4783.         for(i = 0; i <= dupcnt; ++i)
  4784.         {
  4785.           if(*(valp[i]->p) == extfuncop)
  4786.           {
  4787.             *(valp[i]->p) = 0;                    /* convert to nilop */
  4788.             dp = (Pop)((Pop)valp[i]->p)->next;    /* points to thunkblockop */
  4789.  
  4790.             /* Kill the thunkblock */
  4791.             *((char*)dp) = 0;
  4792.             PL(dp->data4) = cdoffset;            /* use this offset for access */
  4793.             PS(((short*)dp)[1]) = cdfile;        /* fileno to unused slots */
  4794.           }
  4795.         }
  4796.     }
  4797.     else if(EFUNC)
  4798.     {/* multiple references to external function */
  4799.     Pop    dp = (Pop)((Pop)valp[0]->p)->next;    /* points to first thunkblockop */
  4800.  
  4801.         cdoffset = GL(dp->data1);            /* save first offset */
  4802.         cdfile = valp[0]->pf->filenum;        /* save first file */
  4803.         for(i = 1; i <= dupcnt; ++i)
  4804.         {/* Kill all thunkblocks except the first */
  4805.             *(valp[i]->p) = 0;                    /* convert to nilop */
  4806.             dp = (Pop)((Pop)valp[i]->p)->next;    /* points to thunkblockop */
  4807.             *((char*)dp) = 0;
  4808.             PL(dp->data4) = cdoffset;            /* use this offset for access */
  4809.             PS(((short*)dp)[1]) = cdfile;        /* fileno to unused slots */
  4810.         }
  4811.     }
  4812.     else if(GBSS)
  4813.     {/* comdefs */
  4814.     int multsize = 0;
  4815.  
  4816.         /* PICK THE BIGGEST GLOBAL BSS (comdef) */
  4817.         for(i = 0; i <= dupcnt; ++i)
  4818.         {
  4819.         Pop dp = (Pop)((Pop)valp[i]->p)->next;    /* points to bssblockop */
  4820.           if((short)dp->data4 && segid == 0)
  4821.           {
  4822.             segid = (short)dp->data4;
  4823.           }
  4824.           else if((short)dp->data4 && (short)dp->data4 != segid)
  4825.           {
  4826.             ++iv->errors;
  4827.             PWARN(pName ": Variable `%s' defined in multiple segments.\n",
  4828.                 valp[i]->symname);
  4829.             PWARN(pName ":  In file: `%s'\n", valp[i]->pf->symaddr[INFILE_SYMNUM]);
  4830.           }
  4831.           if(*(valp[i]->p) == globssop)
  4832.           {
  4833.           long size = GL(dp->data);
  4834.             if(cdsize && size != cdsize)
  4835.                 multsize = 1;
  4836.             if(size > cdsize) {
  4837.                 cdsize = size;
  4838.                 cdoffset = GL(dp->data1);
  4839.                 cdfile = valp[i]->pf->filenum;
  4840.                 cdnum = i;
  4841.             }
  4842.           }
  4843.         }
  4844.         if(GDAT)
  4845.         {
  4846.           /* INITIALIZED DATA WILL ALWAYS OVERRIDE BSS */
  4847.           for(i = 0; i <= dupcnt; ++i)
  4848.           {
  4849.             if(*(valp[i]->p) == glodatop)
  4850.             {
  4851.             Pop dp = (Pop)((Pop)valp[i]->p)->next;    /* points to datablockop */
  4852.             long size = GL(dp->data);
  4853.               if(cdsize && size != cdsize)
  4854.                   multsize = 1;
  4855.               if(size < cdsize)
  4856.               {
  4857.             ++iv->errors;
  4858.             PWARN(pName ": Initialized variable `%s' of size (%d)\n",
  4859.               valp[i]->symname, size);
  4860.             PWARN(pName ":  In file: `%s'\n",
  4861.               valp[i]->pf->symaddr[INFILE_SYMNUM]);
  4862.             PWARN(pName ":  Is incommensurate with common size (%d).\n",
  4863.               cdsize);
  4864.               }
  4865.               else
  4866.               {
  4867.                   cdsize = size;
  4868.                   cdoffset = GL(dp->data1);
  4869.                   cdfile = valp[i]->pf->filenum;
  4870.                   cdnum = i;
  4871.               }
  4872.             }
  4873.           }
  4874.         }
  4875.         if(multsize)
  4876.         {
  4877.           PWARN(pName ":warning: Common Variable `%s' has multiple sizes.\n",
  4878.               valp[0]->symname);
  4879.           for(i = 0; i <= dupcnt; ++i)
  4880.           {
  4881.           unsigned char opcode = *(valp[i]->p);
  4882.             if(opcode == globssop || opcode == glodatop)
  4883.             {
  4884.                 PWARN(pName ":warning:Size=%d in file: `%s'\n",
  4885.                   GL(((Pop)((Pop)valp[i]->p)->next)->data),
  4886.                   valp[i]->pf->symaddr[INFILE_SYMNUM]);
  4887.             }
  4888.           }
  4889.         }
  4890.         /* FINALLY, LINK COMMONS TO THE CHOSEN ONE */
  4891.         for(i = 0; i <= dupcnt; ++i)
  4892.         {
  4893.           if(i != cdnum && *(valp[i]->p) == globssop)
  4894.           {
  4895.           Pop dp = (Pop)((Pop)valp[i]->p)->next;    /* points to bssblockop */
  4896.  
  4897.             *(valp[i]->p) = 0;            /* globssop becomes nilop */
  4898.             *((char*)dp) = 0;            /* bssblockop becomes nilop */
  4899.             PL(dp->data4) = cdoffset;    /* use this new offset for access */
  4900.             PS(((short*)dp)[1]) = cdfile;    /* put fileno in unused slots */
  4901.           }
  4902.         }
  4903.     }
  4904.     else if(GDAT)
  4905.     {
  4906.         for(i = 0; i <= dupcnt; ++i)
  4907.         {
  4908.         Pop dp = (Pop)((Pop)valp[i]->p)->next;    /* points to datablockop */
  4909.           if((short)dp->data4 && segid == 0)
  4910.           {
  4911.             segid = (short)dp->data4;
  4912.           }
  4913.           else if((short)dp->data4 && (short)dp->data4 != segid)
  4914.           {
  4915.             ++iv->errors;
  4916.             PWARN(pName ": Variable `%s' defined in multiple segments.\n",
  4917.                 valp[i]->symname);
  4918.             PWARN(pName ": In file: `%s'\n", valp[i]->pf->symaddr[INFILE_SYMNUM]);
  4919.           }
  4920.           if(*(valp[i]->p) == glodatop)
  4921.           {
  4922.             cdsize = GL(dp->data);
  4923.             cdoffset = GL(dp->data1);
  4924.             cdfile = valp[i]->pf->filenum;
  4925.             cdnum = i;
  4926.             break;
  4927.           }
  4928.         }
  4929.     }
  4930.     if(EVAR && (GDAT || GBSS))
  4931.     {/* match up variables */
  4932.         /* LINK EXTERNS TO THE CHOSEN ONE */
  4933.         for(i = 0; i <= dupcnt; ++i)
  4934.         {
  4935.           if(*(valp[i]->p) == extvarop)
  4936.           {
  4937.           Pop dp = (Pop)valp[i]->p;
  4938.  
  4939.             *((char*)dp) = 0;            /* extvarop becomes nilop */
  4940.             PL(dp->data1) = cdoffset;    /* use this new offset for access */
  4941.             PS(((short*)dp)[1]) = cdfile;    /* put fileno in unused slots */
  4942.             PS(dp->data4) = segid;
  4943.             break;
  4944.           }
  4945.         }
  4946.     }
  4947. #undef GDAT
  4948. #undef GBSS
  4949. #undef GFUNC
  4950. #undef EVAR
  4951. #undef EFUNC
  4952.  
  4953. }
  4954. static void
  4955. link_globals(Piv iv)
  4956. {
  4957.     if(SymHead(iv->gbltbl))
  4958.     {
  4959.     struct _gloval *valp[1024];    /* pointers to symtable value structs */
  4960.  
  4961.       /* Pass over the sorted symbol table and process duplicate entries */
  4962.       while(SymNext(iv->gbltbl))
  4963.       {
  4964.       unsigned long *key;
  4965.       long mark[2];                            /* Table position saver */
  4966.       int dupcnt = 0;
  4967.         SymKey(iv->gbltbl, &key);            /* Pointer to first key */
  4968.         SymValue(iv->gbltbl, &valp[0]);        /* Pointer to first value */
  4969.  
  4970.         while(SymMarkNext(iv->gbltbl, mark))
  4971.         {/* Look forward for duplicates */
  4972.         unsigned long *key1;
  4973.           SymKey(iv->gbltbl, &key1);                /* Pointer to next key */
  4974.           if(KEYEQ(key, key1))
  4975.           {/* Hashed keys match, check the strings */
  4976.             SymValue(iv->gbltbl, &valp[dupcnt+1]);    /* Pointer to next value */
  4977.             if(!strcmp(valp[dupcnt]->symname, valp[dupcnt+1]->symname))
  4978.             {/* Duplicate entry found */
  4979.                 ++dupcnt;
  4980.                 continue;
  4981.             }
  4982.           }
  4983.           break;
  4984.         }
  4985.         if(dupcnt > 0)
  4986.         {/* Process a collection of duplicate symbol names */
  4987.           link_dups(iv, dupcnt, valp);
  4988.         }
  4989.         SymSetMark(iv->gbltbl, mark);
  4990.  
  4991.       }/* END: while(SymNext) */
  4992.     }/* END: if(SymHead) */
  4993. }
  4994. static void
  4995. realloc_data(Piv iv)
  4996. {
  4997. int i;
  4998. Pafile pf;
  4999. unsigned char *p, *prevp;
  5000. unsigned long offset = 0;
  5001.  
  5002.     iv->datatbl = NewSymTable(iv->category, 0);     /* sorted table */
  5003.  
  5004.     for(i = 0; i < iv->numfiles; ++i)
  5005.     {
  5006.         pf = iv->files[i];
  5007.         p = pf->data_p;
  5008.         prevp = 0;
  5009.         while(*p != endfileop)
  5010.         {
  5011.             if(        *p == datablockop
  5012.                 ||    *p == mallocblockop
  5013.                 ||    *p == stringblockop)
  5014.             {
  5015.                 PL(POP->data1) = offset;                
  5016.                 data_insert(iv->datatbl, offset, GL(POP->data), p, prevp);
  5017.                 offset += GL(POP->data);
  5018.                 ROUNDUP(offset, 4);
  5019.             }
  5020.             prevp = p;
  5021.             p = POP->next;
  5022.         }
  5023.     }
  5024.     iv->thunk_offset = offset;
  5025.  
  5026.     for(i = 0; i < iv->numfiles; ++i)
  5027.     {
  5028.         pf = iv->files[i];
  5029.         p = pf->data_p;
  5030.         prevp = 0;
  5031.         while(*p != endfileop)
  5032.         {
  5033.             if(*p == thunkblockop)
  5034.             {
  5035.                 PL(POP->data1) = offset;
  5036.                 data_insert(iv->datatbl, offset, GL(POP->data), p, prevp);
  5037.                 offset += GL(POP->data);
  5038.                 ROUNDUP(offset, 4);
  5039.             }
  5040.             prevp = p;
  5041.             p = POP->next;
  5042.         }
  5043.     }
  5044.     iv->bss_offset = offset;
  5045.  
  5046.     for(i = 0; i < iv->numfiles; ++i)
  5047.     {
  5048.         pf = iv->files[i];
  5049.         p = pf->data_p;
  5050.         prevp = 0;
  5051.         while(*p != endfileop)
  5052.         {
  5053.             if(*p == bssblockop)
  5054.             {
  5055.                 PL(POP->data1) = offset;
  5056.                 data_insert(iv->datatbl, offset, GL(POP->data), p, prevp);
  5057.                 offset += GL(POP->data);
  5058.                 ROUNDUP(offset, 4);
  5059.             }
  5060.             prevp = p;
  5061.             p = POP->next;
  5062.         }
  5063.     }
  5064.     iv->total_size = offset;
  5065. }
  5066. static void
  5067. reset_data_relocs(Piv iv)
  5068. {/* Pass over initialized data and set new offsets in each relocatable slot */
  5069. struct _data {/* datatbl node */
  5070. /* value area 20 bytes */
  5071.     unsigned long size;
  5072.     unsigned char *p;
  5073.     unsigned char unused[12];
  5074. /* key area 8 bytes */
  5075.     unsigned long offset;
  5076.     long unused1;
  5077. };
  5078.     /* PASS OVER ALL THE ENTRIES IN `reloctbl' */
  5079.     if(SymHead(iv->reloctbl))
  5080.     {
  5081.         while(SymNext(iv->reloctbl))
  5082.         {
  5083.         struct _rkey *kp;
  5084.         struct _rval *vp;
  5085.         struct _data *dp, *ndp;
  5086.         unsigned char *p;
  5087.         Pafile pf, npf;
  5088.         unsigned long object_base;
  5089.         int noset = 0;
  5090.  
  5091.           SymKey(iv->reloctbl, &kp);
  5092.           SymValue(iv->reloctbl, &vp);
  5093.           npf = pf = iv->files[kp->fileno];    /* pointer to file struct */
  5094.           p = vp->p;        /* pointer to relocop in input buffer */
  5095.  
  5096.           if(vp->opcode == extlocop)
  5097.           {/* External variable */
  5098.           short key[4];
  5099.           struct {
  5100.           unsigned char *p;    /* pointer to extvarop in input buffer */
  5101.           } *ep;
  5102.             key[0] = vp->rsym;         /* external symbol number */
  5103.             key[1] = pf->filenum;
  5104.             key[2] = 0;
  5105.             key[3] = 0;
  5106.  
  5107.             /* LOOK UP THE EXTERNAL SYMBOL */
  5108.             if(SymFind(iv->extrntbl, key, &ep) && *(ep->p) == 0)
  5109.             {/* symbol exists and the extvarop was filled in */
  5110.  
  5111.                 npf = iv->files[GS( ((short*)(ep->p))[1] )];
  5112.                 PL( POPI->reloc.base ) = GL( ((Pop)(ep->p))->data1 );
  5113.                 *p = relocop;    /* switch input file from `extlocop' */
  5114.             }
  5115.             else
  5116.             {/* Not found or not filled in, leave it alone */
  5117.                 noset = 1;
  5118.             }
  5119.           }
  5120.  
  5121.           /* RESET THE ENTRY IN THE INITIALIZED DATA SLOT */
  5122.           if(SymFindRange(pf->datatbl, &kp->spot, &dp))
  5123.           {/* This entry describes a block of data containg the reloc target */
  5124.           unsigned char *ip = dp->p;    /* points to input buffer */
  5125.           unsigned long extra = kp->spot - dp->offset; /* offset into data */
  5126.  
  5127.  
  5128.             /* Reset the relocop target in the input file */
  5129.  
  5130.             PL( POPI->reloc.spot ) = GL( ((PopI)(ip+8))->s.offset ) + extra;
  5131.  
  5132.             if(vp->rsize == 4)
  5133.             {/* 32 bit relocation */
  5134.             unsigned long *lp;
  5135.  
  5136.                 if(noset)
  5137.                     goto reset32;
  5138.  
  5139.                 lp = (unsigned long*)(ip+24+extra);    /* pointer to target */
  5140.                 object_base = GL( POPI->reloc.base );
  5141.  
  5142.                 /* Find the object that the target points to */
  5143. relink32:
  5144.                 if(SymFindRange(npf->datatbl, &object_base, &ndp))
  5145.                 {
  5146.                     if(*(ndp->p) == 0)
  5147.                     {/* The found object is a discarded thunkblock, relink */
  5148.                         npf = iv->files[GS( ((short*)(ndp->p))[1] )];
  5149.                         object_base = GL( ((Pop)(ndp->p))->data4 );
  5150.                         goto relink32;
  5151.                     }
  5152.                     else
  5153.                     {/* Use the new offset in the input file */
  5154.  
  5155.                         object_base = GL( ((Pop)(ndp->p))->data1 );
  5156.                     }
  5157.                     PL( POPI->reloc.base ) = object_base; /* the `relocop' */
  5158.                     PL(*lp) = object_base + GL( POPI->reloc.offset );/* data */
  5159.                     vp->base = (long)lp;
  5160. reset32:
  5161.                     {
  5162.                     struct _rkey key;
  5163.                     struct _rval val;
  5164.  
  5165.                         key.spot = GL(POPI->reloc.spot);
  5166.                         key.fileno = 0;
  5167.                         val.opcode = *p;
  5168.                         val.rsize = GL(POPI->reloc.rsize);
  5169.                         val.p = p;
  5170.                         val.base = GL(POPI->reloc.base);
  5171.                         val.offset = GL(POPI->reloc.offset);
  5172.                         val.rsym = GS(POPI->reloc.rsym);    
  5173.                         val.fileno = kp->fileno;
  5174.                         SymInsert(iv->newreloctbl, &key, &val, 18);
  5175.                     }
  5176.                 }
  5177.                 else
  5178.                 {
  5179.                     ++iv->errors;
  5180.                     PWARN(pName ":syserr: 32 bit object at offset %d not found\n",object_base);
  5181.                 }
  5182.             }
  5183.             else if(vp->rsize == 2)
  5184.             {/* 16 bit relocation (MORE WORK NEEDED) */
  5185.             unsigned short *sp;
  5186.  
  5187.                 if(noset)
  5188.                     goto reset16;
  5189.                 sp = (unsigned short*)(ip+24+extra);    /* pointer to target */
  5190.                 object_base = GL( POPI->reloc.base );
  5191. relink16:
  5192.                 if(SymFindRange(npf->datatbl, &object_base, &ndp))
  5193.                 {
  5194.                     if(*(ndp->p) == 0)
  5195.                     {/* The found object is a discarded thunkblock, relink */
  5196.                         npf = iv->files[GS( ((short*)(ndp->p))[1] )];
  5197.                         object_base = GL( ((Pop)(ndp->p))->data4 );
  5198.                         goto relink16;
  5199.                     }
  5200.                     else
  5201.                     {/* Use the new offset in the input file */
  5202.  
  5203.                         object_base = GL( ((Pop)(ndp->p))->data1 );
  5204.                     }
  5205.                     PL( POPI->reloc.base ) = object_base; /* the `relocop' */
  5206.                     PS(*sp) = object_base + GL( POPI->reloc.offset );/* data */
  5207. reset16:
  5208.                     {
  5209.                     struct _rkey key;
  5210.                     struct _rval val;
  5211.  
  5212.                         key.spot = GL(POPI->reloc.spot);
  5213.                         key.fileno = 0;
  5214.                         val.opcode = *p;
  5215.                         val.rsize = GL(POPI->reloc.rsize);
  5216.                         val.p = p;
  5217.                         val.base = GL(POPI->reloc.base);
  5218.                         val.offset = GL(POPI->reloc.offset);
  5219.                         val.rsym = GS(POPI->reloc.rsym);    
  5220.                         val.fileno = kp->fileno;
  5221.                         SymInsert(iv->newreloctbl, &key, &val, 18);
  5222.                     }
  5223.                 }
  5224.                 else
  5225.                 {
  5226.                     ++iv->errors;
  5227.                     PWARN(pName ":syserr: 16 bit object at offset %d not found\n", object_base);
  5228.                 }
  5229.  
  5230.             }
  5231.           }
  5232.           else /* !SymFindRange */
  5233.           {
  5234.             ++iv->errors;
  5235.             PWARN(pName ":syserr: reloc not found at %d in file %d\n", 
  5236.                     kp->spot, kp->fileno);
  5237.           }
  5238.         }/* END: While(SymNext) */
  5239.     }/* END: if(SymHead) */
  5240. }
  5241. static void
  5242. reset_offset(Piv iv, Pafile pf, PopA pa)
  5243. {/* All offsets are guaranteed to be inside objects */
  5244. struct _data {/* datatbl node */
  5245. /* value area 16 bytes */
  5246.     unsigned long size;
  5247.     unsigned char *p;
  5248.     unsigned long unused[2];
  5249. /* key area 8 bytes */
  5250.     unsigned long offset;
  5251.     long unused1;
  5252. };
  5253.  
  5254. unsigned long offset;
  5255. struct _data *dp;
  5256. unsigned long object_base;
  5257. long extra;
  5258. unsigned short atype;
  5259. short symnum;
  5260.  
  5261.     offset = GL( pa->offset );
  5262.     atype = GS( pa->atype );
  5263.     symnum = GS( pa->symnum );
  5264.  
  5265.     PS( pa->symnum ) = pf->symtran[symnum];
  5266.     PS( pa->declnum ) = pf->decltran[GS(pa->declnum)];
  5267.  
  5268.     if(atype & A_EXTERN)
  5269.     {
  5270.     short key[4];
  5271.     struct {
  5272.     unsigned char *p;    /* pointer to extvarop in input buffer */
  5273.     } *ep;
  5274.         key[0] = symnum;         /* external symbol number */
  5275.         key[1] = pf->filenum;
  5276.         key[2] = 0;
  5277.         key[3] = 0;
  5278.  
  5279.             /* LOOK UP THE EXTERNAL SYMBOL */
  5280.         if(SymFind(iv->extrntbl, key, &ep) && *(ep->p) == 0)
  5281.         {/* symbol exists and the extvarop was filled in */
  5282.  
  5283.             pf = iv->files[GS( ((short*)(ep->p))[1] )];
  5284.             offset += GL( ((Pop)(ep->p))->data1 );
  5285.         }
  5286.         else
  5287.         {/* Not found or not filled in, leave it alone */
  5288.  
  5289.             return;
  5290.         }
  5291.     }
  5292.     extra = 0;    /* first time through */
  5293.  
  5294.     /* Find the object that the offset points to */
  5295. relink:
  5296.     if(SymFindRange(pf->datatbl, &offset, &dp))
  5297.     {
  5298.         if(extra == 0)
  5299.             extra = offset - dp->offset;
  5300.         object_base = dp->offset;
  5301.  
  5302.         if(*(dp->p) == 0)
  5303.         {/* The found object is a discarded block, relink */
  5304.             pf = iv->files[GS( ((short*)(dp->p))[1] )];
  5305.             offset = GL( ((Pop)(dp->p))->data4 );
  5306.             goto relink;
  5307.         }
  5308.         else
  5309.         {/* Use the adjusted offset in the input buffer */
  5310.  
  5311.             object_base = GL( ((Pop)(dp->p))->data1 );
  5312.         }
  5313.         PL( pa->offset ) = object_base + extra;
  5314.         if(atype & A_EXTERN)
  5315.         {
  5316.             PS( pa->atype ) = atype & ~A_EXTERN;        
  5317.         }
  5318.     }
  5319.     else
  5320.     {
  5321.         ++iv->errors;
  5322.         PWARN(pName ":syserr: object `%s' at offset %d not found\n", 
  5323.             pf->symaddr[symnum], offset);
  5324.     }
  5325. }
  5326. static void
  5327. reset_text_relocs(Piv iv)
  5328. {/* Pass over text and set new offsets in instructions that reference data */
  5329. int i;
  5330.  
  5331.     for(i = 0; i < iv->numfiles; ++i)
  5332.     {
  5333.     Pafile pf;
  5334.     unsigned char *p;
  5335.  
  5336.         pf = iv->files[i];
  5337.         if(!(p = pf->prog_p))
  5338.             continue;
  5339.  
  5340.         while(*p != endfileop)
  5341.         {
  5342.             if(*p && *p <= (unsigned char)100)
  5343.             {/* instruction */
  5344.             int inc = 8;
  5345.                 if((p[1]&0xe0) == OPDATA)
  5346.                     reset_offset(iv, pf, POPA);
  5347.                 inc += (p[1]&0x1f);
  5348.                 if((p[2]&0xe0) == OPDATA)
  5349.                     reset_offset(iv, pf, POPA);
  5350.                 inc += (p[2]&0x1f);
  5351.                 if((p[3]&0xe0) == OPDATA)
  5352.                     reset_offset(iv, pf, POPA);
  5353.             }
  5354.             p = POP->next;                
  5355.         }
  5356.     }
  5357.  
  5358. }
  5359. static void *
  5360. seg_find(Piv iv, int id)
  5361. {
  5362. long key[2];
  5363. void **result;
  5364.  
  5365.     if(iv->segtbl)
  5366.     {
  5367.         key[0] = id;
  5368.         key[1] = 0;
  5369.         if(SymFind(iv->segtbl, key, &result) == 1)
  5370.             return *result;
  5371.     }
  5372.     return 0;    
  5373. }
  5374. static void
  5375. check_seg(Piv iv, unsigned char *p, Pafile pf)
  5376. {
  5377. PopI np, op;
  5378.     if(!(iv->segtbl))
  5379.     {
  5380.         iv->segtbl = NewSymTable(iv->category, 111);
  5381.     }
  5382.     if((op = seg_find(iv, GS(POPI->segdef.segid))))
  5383.     {
  5384.         np = POPI;
  5385.         if(        GL(np->segdef.v1) == GL(op->segdef.v1)
  5386.             &&    GL(np->segdef.v2) == GL(op->segdef.v2)
  5387.             &&    GL(np->segdef.v3) == GL(op->segdef.v3))
  5388.         {/* segments of same name have the same values */
  5389.             *p = 0;    /* kill the new definition */
  5390.             return;
  5391.         }
  5392.         else
  5393.         {/* segments of same name have different values */
  5394.             ++iv->errors;
  5395.             PWARN(pName ":Segment `%s' defined differently.\n",
  5396.                 iv->symaddr[GS(POPI->segdef.segid)]);
  5397.             PWARN(pName ":  In file: `%s'\n", pf->symaddr[INFILE_SYMNUM]);
  5398.             return;
  5399.         }
  5400.     }
  5401.     else
  5402.     {
  5403.     long key[2];
  5404.     PopI pp = POPI;
  5405.         key[0] = GS(POPI->segdef.segid);
  5406.         key[1] = 0;
  5407.         SymInsert(iv->segtbl, key, &pp, 4);
  5408.     }
  5409. }
  5410. static void
  5411. reset_syms_decls(Piv iv)
  5412. {
  5413. int i;
  5414.     for(i = 0; i < iv->numfiles; ++i)
  5415.     {
  5416.     Pafile pf;
  5417.     unsigned char *p;
  5418.  
  5419.         pf = iv->files[i];
  5420.         p = pf->file_p;
  5421.  
  5422.         while(*p != endfileop)
  5423.         {
  5424.           if(*p == segdefop)
  5425.           {
  5426.             PS(POPI->segdef.segid) = pf->symtran[GS(POPI->segdef.segid)];
  5427.             check_seg(iv, p, pf);
  5428.           }
  5429.           else if(i > 0)
  5430.           {
  5431.             switch(*p)
  5432.             {
  5433.                 case    declop:
  5434.                     if(GS(POPI->dcl.declnum) < 22)
  5435.                     {/* kill the base declarations */
  5436.                         *p = 0;
  5437.                         p = POP->next;                
  5438.                         *p = 0;
  5439.                     }
  5440.                     else
  5441.                      PS(POPI->dcl.declnum)=pf->decltran[GS(POPI->dcl.declnum)];
  5442.                     break;
  5443.                 case    extlocop:
  5444.                     PS(POPI->reloc.rsym) = pf->symtran[GS(POPI->reloc.rsym)];
  5445.                     break;
  5446.                 case    gfuncdefop:
  5447.                 case    sfuncdefop:
  5448.                     if(pf->numsegs)
  5449.                     PS(POPI->funcdef.segid) = pf->symtran[GS(POPI->funcdef.segid)];
  5450.                 case    nestedfuncdefop:
  5451.                     PL(POPI->funcdef.symnum)=pf->symtran[GL(POPI->funcdef.symnum)];
  5452.                     break;
  5453.                 case    bssblockop:
  5454.                 case    datablockop:
  5455.                     if(pf->numsegs)
  5456.                     PS( POPI->s.segid ) = pf->symtran[GS(POPI->s.segid)];
  5457.                 case    stringblockop:
  5458.                 case    mallocblockop:
  5459.                 case    thunkblockop:
  5460.                 case    extvarop:
  5461.                     PS( POPI->s.symnum ) = pf->symtran[GS(POPI->s.symnum)];
  5462.                     PS( POPI->s.declnum ) = pf->decltran[GS(POPI->s.declnum)];
  5463.                     break;
  5464.                 case    memberinfop:
  5465.                 case    bfieldinfop:
  5466.                     PS(POPI->memb.symnum) = pf->symtran[GS(POPI->memb.symnum)];
  5467.                     PS(POPI->memb.declnum) = pf->decltran[GS(POPI->memb.declnum)];
  5468.                     PS(POPI->memb.cdeclnum) = pf->decltran[GS(POPI->memb.cdeclnum)];
  5469.                     break;
  5470.                 case    structinfop:
  5471.                     PS(POPI->suinf.symnum) = pf->symtran[GS(POPI->suinf.symnum)];
  5472.                     break;
  5473.                 case    funcptrinfop:
  5474.                 case    ptrinfop:
  5475.                     PS(POPI->ptrinf.declnum) = pf->decltran[GS(POPI->ptrinf.declnum)];
  5476.                     break;
  5477.                 case    funcinfop:
  5478.                     PS(POPI->funcd.declnum) = pf->decltran[GS(POPI->funcd.declnum)];
  5479.                     PS(POPI->funcd.symnum) = pf->symtran[GS(POPI->funcd.symnum)];
  5480.                     break;
  5481.                 case    arrayinfop:
  5482.                     PS(POPI->ary.declnum) = pf->decltran[GS(POPI->ary.declnum)];
  5483.                     break;
  5484.                 case    lineop:
  5485.                     PL(POPI->line.filenamenum) = pf->symtran[GL(POPI->line.filenamenum)];
  5486.                     break;
  5487.             }/* END: switch(*p) */
  5488.           }/* END: i > 0 */
  5489.           p = POP->next;                
  5490.         }
  5491.     }
  5492. }
  5493. static int
  5494. link_files(Piv iv)
  5495. {
  5496.     iv->extrntbl = NewSymTable(iv->category, 4092);    /* hashed table */
  5497.     iv->reloctbl = NewSymTable(iv->category, 4092); /* hashed table */
  5498.     iv->newreloctbl = NewSymTable(iv->category, 4092); /* hashed table */
  5499.     iv->gbltbl = NewSymTable(iv->category, 0);    /* sorted table */
  5500.  
  5501.     setup_syms_decls(iv);
  5502.  
  5503.     if(iv->numfiles > 1)
  5504.     {
  5505.         iv->symaddr = Ccalloc(iv->category, sizeof(void*), iv->numsyms);
  5506.         iv->decladdr = Ccalloc(iv->category, sizeof(void*), iv->numdecls);
  5507.         iv->symtbl = NewSymTable(iv->category, 0); /* sorted table */
  5508.         combine_syms_decls(iv);
  5509.  
  5510.         link_globals(iv);
  5511.         realloc_data(iv);
  5512.         reset_data_relocs(iv);
  5513.         reset_text_relocs(iv);
  5514.  
  5515.         reset_syms_decls(iv);
  5516.     }
  5517.     else
  5518.     {
  5519.         iv->symaddr = iv->files[0]->symaddr;
  5520.         iv->decladdr = iv->files[0]->decladdr;
  5521.         iv->symtbl = NewSymTable(iv->category, 0); /* sorted table */
  5522.         combine_syms_decls(iv);
  5523.  
  5524.         realloc_data(iv);
  5525.         reset_data_relocs(iv);
  5526.         reset_text_relocs(iv);
  5527.     }
  5528.     return iv->errors;
  5529. }
  5530.  
  5531. /* ======================== GLOBAL ROUTINES ========================== */
  5532. int
  5533. Global(readfile) (Piv iv, char *infile_name)
  5534. {
  5535. FILE *infile;
  5536. long infile_size;
  5537. char *inbuf;
  5538.  
  5539.     iv->infile_name = infile_name;
  5540.     if(!(infile = fopen(infile_name, "rb")))
  5541.     {
  5542.         PERROR(pName ":ERROR: Can't open input file: %s\n", infile_name);
  5543.     }
  5544.     fseek(infile, 0, SEEK_END);
  5545.     infile_size = ftell(infile);    
  5546.     fseek(infile, 0, SEEK_SET);
  5547.  
  5548.     if(infile_size == 0)
  5549.     {
  5550.         PERROR(pName ":ERROR: Empty input file: %s\n", infile_name);
  5551.     }
  5552.     inbuf = Cmalloc(iv->category, infile_size);
  5553.  
  5554.     if(fread(inbuf, 1, infile_size, infile) != infile_size)
  5555.     {
  5556.         fclose(infile);
  5557.         PERROR(pName ":ERROR: reading input file: %s\n", infile_name);
  5558.     }
  5559.     fclose(infile);
  5560.  
  5561.     if(setup_nodelinks(iv, infile_name, inbuf, infile_size))
  5562.         return 4;
  5563.     return 0;
  5564. }
  5565. int
  5566. Global(proc_files) (Piv iv, void *name)
  5567. {
  5568. int ret;
  5569.  
  5570.     if(!(ret = link_files(iv)))
  5571.     {
  5572.         optimize(iv);
  5573.         if(name)
  5574.           iv->symaddr[2] = name;    /* symbol 2 is the output filename */
  5575.         ret = gen_output(iv, iv->symaddr[2]);
  5576.     }
  5577.     return ret;
  5578. }
  5579. void *
  5580. Global(open_instance) (void)
  5581. {
  5582. Piv iv;
  5583. int category;
  5584. #if USING_FRAMEWORK
  5585.     if(num_instance <= 0)
  5586.     {
  5587.         oxlink_clear_bss(pName ".o");    /* reset global storage */
  5588.         local_category = NewMallocCategory();
  5589.     }
  5590.     ++num_instance;
  5591. #endif
  5592.     category = Cnewcat();
  5593.     iv = Ccalloc(category, 1, sizeof(struct _iv));
  5594.     iv->category = category;
  5595.     return iv;
  5596. }
  5597. void
  5598. Global(close_instance) (Piv iv)
  5599. {
  5600.     if(iv->outfile)
  5601.       fclose(iv->outfile);
  5602.     if(iv->remove_infile)
  5603.     {
  5604.     int i;
  5605.       for(i = 1; i < iv->argc; ++i)
  5606.         unlink(propernameof(iv, iv->argv[i]));
  5607.     }
  5608.     Cfreecat(iv->category);
  5609. #if USING_FRAMEWORK
  5610.     if(--num_instance == 0)
  5611.         freecat(local_category);
  5612. #endif
  5613. }
  5614.  
  5615. /* =========================== THE MAIN PROGRAM ======================= */
  5616.  
  5617. static char *
  5618. filenameof(char *path)
  5619. {
  5620. char *ret = path;
  5621. int i;
  5622.     for(i = 0; path[i]; ++i)
  5623.       if(path[i] == '/')
  5624.         ret = &path[i+1];
  5625.     return ret;
  5626. }
  5627.  
  5628. static char *
  5629. propernameof(Piv iv, char *path)
  5630. {
  5631. char *name = filenameof(path);
  5632. int namlen = strlen(name);
  5633. int i;
  5634.     for(i = namlen-1; i >= 0; --i)
  5635.     {
  5636.       if(name[i] == '/' || name[i] == '\\')
  5637.           break;
  5638.       else if(name[i] == '.')
  5639.         return path;
  5640.     }
  5641.     name = Cmalloc(iv->category, strlen(path)+8);
  5642.     strcpy(name, path);
  5643.     strcat(name, ".anf");
  5644.     return name;
  5645. }
  5646. static void
  5647. Usage()
  5648. {
  5649. fputs(
  5650. "Usage: " pName " [-odsLDR?] [infile...]\n"
  5651. "   -o outfile == name of output file\n"
  5652. "   -d == print debug output\n"
  5653. "   -D == only print debug output\n"
  5654. "   -s == strip declarations and line numbers\n"
  5655. "   -L == generate listing only (to .lst)\n"
  5656. "   -R == remove the input file\n"
  5657. "   -? == print this message\n"
  5658. "   Default input file is `code.anf'.\n"
  5659. "   Default output file is specified by the input.\n"
  5660. ,stderr);
  5661. }
  5662.  
  5663. #if USING_FRAMEWORK
  5664. int
  5665. PROG (int argc, char **argv)
  5666. #else
  5667. int
  5668. main (int argc, char **argv)
  5669. #endif
  5670. {
  5671. int i,j;
  5672. char *outfilename = 0;
  5673. volatile Piv iv;
  5674. char debug, only_debug, strip, listing_wanted, remove_infile;
  5675. int ret;
  5676.  
  5677.     remove_infile = listing_wanted = strip = debug = only_debug = 0;
  5678.  
  5679.     /* Get options */
  5680.     for(i = 1; i < argc; ++i)
  5681.     {
  5682.     int trimsize = 1;
  5683.         if(argv[i][0] == '-')
  5684.         {
  5685.             for (j=1; argv[i][j]; j++)
  5686.             {
  5687.                 switch(argv[i][j])
  5688.                 {    
  5689.                     case    'D':
  5690.                         only_debug = 1;
  5691.                         /* FALL THROUGH */
  5692.                     case    'd':
  5693.                         debug = argv[i][++j];
  5694.                         break;
  5695.                     case    's':
  5696.                         strip = 1;
  5697.                         break;
  5698.                     case    'o':
  5699.                         if(argv[i][j+1]) {
  5700.                             outfilename = &argv[i][j+1];
  5701.                         }
  5702.                         else if(i < argc-1) {
  5703.                             outfilename = argv[i+1];
  5704.                             trimsize = 2;
  5705.                         } else {
  5706.                             PWARN(pName ": no output filename\n");
  5707.                             Usage();
  5708.                             return 0;
  5709.                         }
  5710.                         goto trim;
  5711.                         break;
  5712.                     case    'L':
  5713.                         listing_wanted = 1;
  5714.                         break;
  5715.                     case    'R':
  5716.                         remove_infile = 1;
  5717.                         break;
  5718.                     case '?':
  5719.                         Usage();
  5720.                         return 0;
  5721.                     default:
  5722.                         PWARN(pName ": Invalid switch: %c\n", argv[i][j]);
  5723.                         Usage();
  5724.                         return 0;
  5725.                 }
  5726.             }/* END: for(j) */
  5727. trim:
  5728.             /* Trim switch */
  5729.             for(j = i; j < argc-trimsize; ++j)
  5730.                 argv[j] = argv[j+trimsize];
  5731.             argc -= trimsize;
  5732.             --i;
  5733.         }/* END: if('-') */
  5734.     }/* END: for(argc) */
  5735.  
  5736.     iv = Global(open_instance) ();
  5737.     if((ret = setjmp(run_env))) {
  5738.         Global(close_instance) (iv);
  5739. #if USING_FRAMEWORK
  5740.         return ret;
  5741. #else
  5742.         exit(ret);
  5743. #endif
  5744.     }
  5745.     iv->debug = debug;
  5746.     iv->only_debug = only_debug;
  5747.     iv->labeltbl = NewSymTable(iv->category, 4092);
  5748. #if REALLY_NEED_OFFSETS
  5749.     iv->newlabeltbl = NewSymTable(iv->category, 4092);
  5750. #endif
  5751.     iv->strip = strip;
  5752.     iv->listing_wanted = listing_wanted;
  5753.     iv->remove_infile = remove_infile;
  5754.     iv->argc = argc;
  5755.     iv->argv = argv;
  5756.  
  5757.     if(argc < 2)
  5758.     {/* Default input filename is 'code.anf' */
  5759.         ret = Global(readfile) (iv, "code.anf");
  5760.     }
  5761.     else
  5762.     {/* READ EACH INPUT FILE */
  5763.     
  5764.         for(i = 1; i < argc; ++i)
  5765.           if((ret = Global(readfile) (iv, propernameof(iv,argv[i]))))
  5766.             break;
  5767.     }
  5768.     if(!ret && !iv->only_debug)
  5769.     {
  5770.         ret = Global(proc_files) (iv, outfilename);
  5771.     }
  5772.     Global(close_instance) (iv);
  5773. #if USING_FRAMEWORK
  5774.     return ret;
  5775. #else
  5776.     exit(ret);
  5777. #endif
  5778. }
  5779.  
  5780.